Why does setSessionContext() get called before ejbCreate()

Discussions

EJB programming & troubleshooting: Why does setSessionContext() get called before ejbCreate()

  1. Hi all,

    I invoked a method on a stateless session bean from a client file and saw in the logs that the order in which the methods got called was:

    1. setSessionContext
    2. ejbCreate
    3. <method I was trying to invoke>

    My query is: Why does setSessionContext method get called before ejbCreate()? It is so because till we invoke ejbCreate(), EJB Context is not created. And ,moreover, when is the SessionContext object actually created?

    Regards,

    Alongian
  2. Hi kumar,

    First, The motivation behind an EJBContext is to encapsulate the bean’s domain in one
    compact object. Note that a baen’s status may change over the bean’s life cycle,
    and thus this context object can dynamically change over time as well. At runtime,
    the container is responsible for changing the context to reflect any status
    changes, such as the bean becoming involved in a new transaction.
    So for our example it's normal that the container calls setSessionContext() before
    the ejbCreate() method (note that in your ejbCreate() method you can need to access your SessionContext!)

    BENMIRA NABIL
  3. Hi

    The Container calls setSessionontext method to associate your bean with a session context.
    A session context is u'r beans gateway to interact with the container, u'r bean can use session context
    to query the container abt u'r current transactional state,u'r current security state..

    where as ejb create is used to initialize u'r beans .


    Mushtaq
  4. Thanks Mustaq and BENMIRA. I was going thru some book and found that setSessionContext is actually called before the bean(Referring to Stateless session beans only) goes in Pooled state(it is not attached to any EJB object till now) and ejbCreate() is called before beans go in Method-Ready state(when it is ready to serve a client). Now if my understanding is correct: SessionContext object can be used to gain information about the container, about the bean and about the client also. About the container and the bean I see no issues. But how can I gain info about the client using the SessionContext object when setSessionContext is invoked before the bean is actually attached to any EJB object?
  5. Information about the client[ Go to top ]

    The getCallerPrincipal() method in the SessionContext can be used to get information about the caller .The method type is of java.security.Principal
  6. But how can I gain info about the client using the SessionContext

    > object when setSessionContext is invoked before the bean is
    > actually attached to any EJB object?

    You (i.e. the client) wouldn't. But the *bean* might need to make use of the SessionContext upon creation, and so it has to actually be there before ejbCreate is invoked.
  7. Agreed Ian. Now, I do not have any doubts in why setSessionContext() is invoked before ejbCreate(). What I want to know is how actually a bean comes to know about the client it is servicing(through the EJB Object)? To extend it further, how does a bean actually gather information about the client.
  8. The container is telling the bean about its client through the session context (getCallerPrincipal, isCallerInRole, getUserTransaction).