Is a stateless session bean's remote handle thread-safe?

Discussions

EJB programming & troubleshooting: Is a stateless session bean's remote handle thread-safe?

  1. I know that you can cache the Home interface and call create() each time you want to use a stateless session bean.

    Can you just cache the handle that Home.create() returns?

    I don't think you should do this, but one of my coworkers did it, and I can't find a problem with it (except for the fact that you can't change the bean to stateful).

    The container assigns a new instance to every method call on its end, so is there a synchronization issue on the client stub end?

    I'm using WebLogic.
  2. What you get from create() methods is an EJBObject, not a Handle, so I'll assume that's what you ment.
    It is OK to make concurrent calls to an EJBObject of a stateless session bean. The restriction of single-threaded access is only valid for stateful session beans. In the stateless case, the container simply routes each request to a different instance (see subsection 7.5.6 of EJB2.0 spec).

    However, there is still a risk to using SLSBs this way - the different calling threads will (most probably) have different transaction contexts. You need to code you application carefully so that you don't assume operations of one thread are visible to the other.

    Hope that helps.
    Gal.
  3. Thank you.