wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Where to put initialization code for SLSB or MDB? ejbCreate or setXXXContext? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Where to put initialization code for SLSB or MDB? ejbCreate or setXXXContext?" Watch "Where to put initialization code for SLSB or MDB? ejbCreate or setXXXContext?" New topic
Author

Where to put initialization code for SLSB or MDB? ejbCreate or setXXXContext?

Dani Mazzuca
Ranch Hand

Joined: May 21, 2003
Posts: 70
Hi,
Which is the most appropriate place to put a code that caches a JNDI InitialContext object or a JDBC data source object for SLSB or MDB? Is it setXXXContext() or ejbCreate() method? Pros and cons for each one? (please, for this time forget the ServiceLocator pattern; I just want to know where is the most appropriate place to put initialization code).
Thanks in advance,
Dani Mazzuca
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
I think ejbCreate() will be appropiate. Since setXXXContext() is called before the ejbCreate(), it is, as of its name, better use for context issues. For caching the JNDI lookup instance, it is better to be done inside ejbCreate().
Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Ramakrishna Allam
Ranch Hand

Joined: Mar 23, 2004
Posts: 34
I think we can do both the ways as everybody know . But It might be more appropriate to do it in the setXXXContext() methods . I remember i read some where in the spec , that we will write code that required for the life time of the bean in setXXContext() methods . But I read that for entity beans . Should be same for session as well as MDB . Any comments?
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
setSessionContext(), - as long as you do not call any methods on the Data source object, it is fine...
Baskaran Vengesanam
Greenhorn

Joined: Apr 02, 2004
Posts: 7
I would say, ejbCreate() method is the right place to have "Initialization code", no matter it is for SLSB, SFSB or MDBs. The reason being, you can do anything you need to from this method, including things you can't do in setSessionContext(). See HFE Page - 195 (point 3).
Hope, this answers your question.
On the way to certification.
Dani Mazzuca
Ranch Hand

Joined: May 21, 2003
Posts: 70
Hi Baskaran,
Originally posted by Baskaran Vengesanam:
I would say, ejbCreate() method is the right place to have "Initialization code", no matter it is for SLSB, SFSB or MDBs. The reason being, you can do anything you need to from this method, including things you can't do in setSessionContext(). See HFE Page - 195 (point 3).
Hope, this answers your question.
On the way to certification.

I also think that it is better to put the code in ejbCreate() method, although I am not sure of that. However, note that HFE page 195 (point 3) is referring to Stateful session beans, especially por the purpose of passing arguments values to the bean. This is not the case for SLSB and MDB.
Dani Mazzuca
 
 
subject: Where to put initialization code for SLSB or MDB? ejbCreate or setXXXContext?