Behaviour of EJB under multi thread access

Discussions

EJB design: Behaviour of EJB under multi thread access

  1. Consider the case when a EJBObject stub (Remote object looked up through home object)is invoked by multiple threads. What would be the result .Do we get expected results from the EJB or an exception?

    Give comments??
  2. Here's my understanding of the EJB2.1 spec:

    Several factors influence the behaviour of multithreaded calls to an EJBObject.
    First, if the bean is a stateful session bean, then either an exception is thrown, or the second invocation is queued.
    If the bean is an entity bean, the results depend on the transaction in context at each call. If the threads operate within the same transaction, and the bean is not reentrant, an exception is thrown. If it is reentrant, then an exception may or may not be thrown. Anyway this is considered an application error and has unspecified results.
    If each thread has a different transaction context (or atleast one of the threads has no transaction context) then the two calls operate as if they had originated in different client (as far as the EJB spec is concerned, they did).

    Also note that while concurrent invocation on a single stateful session bean instance is detected and signaled by the App server, errors can still arise when two threads call two different session beans, and each of them reads/writes the same data from the DB. There is no way for the container to detect this, so you as a programmer have to make sure you avoid any problems arising from such a situation.

    I hope this clarifies things.
    Gal
  3. Great inputs Gal.

    Thanks,
    KK