How to do synchronization in SessionBean?

Discussions

EJB design: How to do synchronization in SessionBean?

  1. How to do synchronization in SessionBean? (4 messages)

    Hello EJB gurus,

    I have a doubt.
    Are the methods implemented in the SessionBean thread safe by default?

    If they are not thread safe then can I make a function thread safe by writing the keyword synchronized before the function name. Is it allowed in EJBs.

    Thanks
    Ravi

  2. YES. See the "Enterprise JavaBeans™
    Specification, v1.1" (rtfm) for details:

    "The container must ensure that only one thread can be executing an instance at any time. If a client
    request arrives for an instance while the instance is executing another request, the container must throw
    the java.rmi.RemoteException to the second request.
    Note that a session object is intended to support only a single client. Therefore, it would be an
    application error if two clients attempted to invoke the same session object."
  3. Bear in mind that two physical clients could be one logical client.

    This could happen in thevent that you have multiple frames or in the event that a page is submitted, stopped and resubmitted.

    Aaron Robinson
    J2EE Architect
  4. I think the container creates a different instance for different client in the case of stateful session bean.

    I want to know whether the container creates a different instance for different client or not in the case of stateless session bean.

    If the container does not create a different instance for different user then it will have synchronization problems in the case of stateless session bean.

    So I want to know whether I can write the keyword synchronized keyword before the method name in the bean implementation.

    Please clarify my doubt.

    Thanks
    Ravi
  5. How to do synchronization in SessionBean?[ Go to top ]

    Hi,
    I think the container creates a different instance for different client in the case of stateful session bean.

    * yes

    I want to know whether the container creates a different instance for different client or not in the case of stateless session bean.

    * no

    If the container does not create a different instance for different user then it will have synchronization problems in the case of stateless session bean.

    * as long as a method call is being executed, the container wud not allocate the same instance for another client. once the method execution is over, the instance returns to the pool. so, synchronization in SLSB is taken care by the container.


    So I want to know whether I can write the keyword synchronized keyword before the method name in the bean implementation.