GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes how should the optimized DAOs used in Stateless Session Bean look like? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "how should the optimized DAOs used in Stateless Session Bean look like?" Watch "how should the optimized DAOs used in Stateless Session Bean look like?" New topic
Author

how should the optimized DAOs used in Stateless Session Bean look like?

Robert Strong
Ranch Hand

Joined: Sep 10, 2002
Posts: 84
hi, I recently studied a sample J2EE applications from Oracle, where DAO is used to provide persistence, and stateless session bean used for business logic. But I'm a little bit confused about whether this is an optimized DAO design.

in DAOFactory and getDAO() method is like below:

public class TradeManagementDAOFactory {
private static TradeManagementDAO dao = null;
public static TradeManagementDAO getDAO() throws Exception {
if( dao == null) {
dao = (TradeManagementDAO) Class.forName(implClassName).newInstance();
}
return dao;
}
}
So, only one DAO instance would be created, all requests to DAO would actually share the same DAO instance.


the DAO class use an instance variable to keep track of DataSource, and in each instance method of DAO class, a connection is acquired and then released.


In Stateless Session Bean like below, an instance variable is used to keep track of the DAO instance, which is initialized in ejbCreate():

public class TradeManagementSessionFacadeBean implements javax.ejb.SessionBean {
private TradeManagementDAO tradeManagementDAO;
....
}

So, in a multi-user environment, where there're multiple transactions and multiple Stateless Session Bean instances, multiple SLSB instances or transactions would share the same DAO instance? is this a good, optimized DAO desgin pattern?
vu lee
Ranch Hand

Joined: Apr 19, 2005
Posts: 189
It's optimized. Since states are maintained within the method, it is fine for multithreading/multitransaction. DAO instance is somewhat like servlet--there is a single instance of servlet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how should the optimized DAOs used in Stateless Session Bean look like?