This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic