Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Robert Strong
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic