question about setSessionContext and ejbCreate
hi all
i have a question regarding these two methods. in my EJB, i need to lookup another EJB. should i put the lookup stuff in setSessionContext or ejbCreate method? i see a lot of samples that put initailization stuff such as look up another EJB in setSessionContext. if that is the case, what is the purpose of ejbCreate? thanks
[344 byte] By [
yu169409a] at [2007-10-3 5:00:01]

Both are required to have access to the private component naming environment
(java:comp/env) so in terms of looking up an ejb reference it doesn't really matter.
ejbCreate() doesn't really serve any purpose for Stateless session beans.
For Stateful session beans it allows state to be passed in by the client. Likewise
for CMP/BMP entity beans.
BTW, this is all vastly simplified in the EJB 3.0 programming model. You can
get EJB references using the @EJB annotation and they are automatically
injected by the container.This way you don't need a setSessionContext()
method or an ejbCreate() method.In the Stateful session bean case, you
can just define a business method that sets the desired values instead of
using ejbCreate.
--ken
ksaksa at 2007-7-14 23:05:29 >

for ejb2. we used ejbCreate to create ejb references and it worked fine.
ynroya at 2007-7-14 23:05:29 >

thanks. i will be using EJBGen to generate the bean, home and remote interfaces.
so you are saying that i don't need to do lookup to get reference of other EJBs if i use @ejbgen:ejb-ref?
can u provide some samples? if i have the following
* @ejbgen:session
*max-beans-in-free-pool = 10
*initial-beans-in-free-pool = 0
*trans-timeout-seconds = 0
*home-is-clusterable = True
*type = Stateless
*home-load-algorithm = RoundRobinAffinity
*transaction-type = Container
*default-transaction = Required
*enable-call-by-reference = True
*ejb-name = myEJB
*
* @ejbgen:jndi-name
*remote = mybeanname
*
* @ejbgen:ejb-ref
*name = ejb/something
*home = somethingHome
*remote = something
*type = Session
*link = somethingEJB
*jndi-name = aname
*/
public class MyEJB implements SessionBean{
//how do i get reference of somethingEJB?
}
You're still using EJB 2.x.I was just pointing out that moving to EJB 3.0
will vastly simplify things so you don't have to use EJBGen.You can
find more information on EJB 3.0 here :
http://java.sun.com/javaee/5/docs/tutorial/doc/
https://glassfish.dev.java.net/javaee5/ejb/EJB30.html
--ken
ksaksa at 2007-7-14 23:05:29 >

after checking out the sample at glassfish. EJB3 is very simple. unfortunately i am stuck with EJB2 for the moment.
so back to my original question. so it doesn't really matter where i put the initialization stuff such as getting a reference of another EJB or JDBC connection or creating a DAO instance so long as it is a stateless session EJB.
If it's just a lookup, either one will work.From a stylistic point of view, ejbCreate is probably a
better choice since from a semantic standpoint it's the initialization method for the instance.
setSessionContext() is just a way to deliver the SessionContext object to the instance in
EJB 2.x (and earlier).
--ken
ksaksa at 2007-7-14 23:05:29 >

thanks for ur prompt reply. i got the point now. i will put those in ejbCreate
One advantage of putting initialization code in setSessionContext() is that when u are working with stateful session beans, there can be more then 1 ejbCreate().
Since there is always only 1 setSessionContext() method, the initialization like looking up other EJB's is often done there to avoid writing same code more then once.