EJB programming & troubleshooting: Methods synchronization in Enterprise JavaBean

  1. Are the methods in the session bean synchronized by the container automatically. Can I manually add the synchronized key word in my code
  2. The container guarantees serialization of bean methods, only a single thread can call it. There is no chance of concurrent invocation. You can leave the synchronized keyword off.
  3. In addition, how the container disposes the static method the the ejb.
  4. Stephen,

    can you point me to the part of the spec where this is stated?

    And - just to be sure - do you mean, that on a single instance this holds true, or on all instances of the same bean (class-method-like)?

  5. Yeah, I was puzzled by the same question!
  6. This behaviour can be specified at deploy time; more than one thread may be in a given exposed method of a "re-entrant" EJB. You usually need a re-entrant EJB under very complex situations and it is almost always avoidable.

    I don't remember anything about static methods in the EJB spec but my hunch says this is not possible... You cannot declare a static method in an interface so you cannot possibly use a static method from the client.
  7. Bean by itself does't extend Thread/Runnable, container is
    multithreaded and creates multiple instances, one per request ( there is limit).
  8. The container will never allow *two seperate transactions* to execute in the same bean instance at the same time. If the bean is an entity bean and is declared to be re-entrant, the container will allow seperate requests *from the same transaction* to execute on the same instance at the same time. Otherwise, the container won't even allow two requests from the same transaction to execute on a single instance at the same time.
    Note that all of this only holds for methods calls that go through the container, i.e methods that are invoked on the remote interface of the bean. Shouldn't make that big a difference in practice, but good to know...

  9. Ok,
    easiest answers first.

    from EJB 2.0 spec, 7.11.8 "The container must ensure that only one thread can be executing an instance at any time"

    This holds true for all session beans.

    The number of stateless session bean instances represents the maximum concurrency level within the container. Controlling the number of SLSB's is a vendor specific feature.

    as Ferhat points out, you can't have static methods in a remote interface. The container will not serialize access to static methods. What is it that you're trying to do here? Are you implementing a Singleton? Static variables are discouraged in a J2EE container. Besides, statics can cause problems if/when you decide to cluster.

  10. Thanks Gal, for correcting me. A single thread may re-enter the same re-entrant EJB in the same transaction, as in:

    EJBA(instance 1).a() {

    EJBB.d(pk) {
     instanceOfEJBA = findByPrimaryKey(pk); // returns a ref to EJBA(instance 1)


    In this case, EJBA must be re-entrant.