Hi This is from Whizlabs again. When would you use the DAO pattern in regards to a Stateful Session Bean? Ans is : You wdn't use DAO with SFSB Because it is expensive. My question is But if you were to do Database read/write , wouldn't it be appropriate to use DAO? Thanks Debanjana
I don't entirely agree with whiz labs. One of the purposes of DAO is to uncouple the biz tier from a DB implementation. SFSB to DAO is local references and I don't see a huge performance hit. What does the rest of the peanut gallery think?
This question has come up before, however I am too lazy to search for that thread... Possibly, the intention of the Whiz Labs' answer was that one should not be doing database access from within a Stateful Session Bean. In general, that is good advice but I would never make such a blanket statement. In most cases SFSB is not a good choice for database heavy activities, however there are situations where it is. One thing that makes SFSBs powerful, and therefore a viable choice at times, is their ability to interact with transactions via the SessionSynchronization interface. To get back to the question at hand, if I were to access a database from a SFSB then I would certainly use DAO to encapsulate that activity. I completely agree with Rufus's assessment of the purpose of DAO.
As Chris says, its not appropriate to say that you would never use SFSB with DAOs. But as Chris also points out, the author meant that from a best practices point of view, rather than a feasibility point. Although SUN would vouch for SFSB, I have not seen too many EJB implementations where SFSB are even used. They are expensive to maintain. Also (according to the best practices) you would use DAOs to retrieve data (and possibly update data that is non-concurrent). Could you use a SFSB to manage the retrieval/update, yes you could. But if your app already has a SFSB for managing state, you will probably leverage it from your Session Managers (Stateless beans that interact with DAOs). Hope this helps.
Sanjay Raghavan<br />SCJP2, SCEA-J2EE<br />Moderator - <a href="http://groups.yahoo.com/group/scea_prep" target="_blank" rel="nofollow">SCEA PREP</a><br />Co-Author - <a href="http://www.whizlabs.com/scea/scea.html" target="_blank" rel="nofollow">SCEA@Whiz</a><br /><i>Where did you sip your Java Today?</i>