Java EE EJB concurrency (ConcurrencyManagement, Lock and LockType)

Datetime:2016-08-23 01:58:23          Topic: EJB           Share

Even though Stateless EJB's may never be concurrently accessed by more than a single client call, this article would be otherwise incomplete if it did not mention this kind of EJB's.

Stateless EJB's are pooled by the container. Every time a client makes a call to a Stateless EJB, the container will fetch an available instance from the pool to handle the client request. When that instance is handling the client request, that same instance will not handle any other call that is made from any given client. When that call ends, the EJB instance is returned to the pool and becomes once again available to handle client requests.

If there is no available EJB instance in the pool to handle a client request (ex: the pooled EJB instances are all busy handling other requests), the container will create a new instance, put it in the pool, and let it handle the incoming client request.

This pool is usually configured by application server, and if it becomes exhausted when a client request arrives (ex: the pool has reached its maximum size) an exception will be thrown and propagated to the remote client.

Even if there are multiple consecutive calls from the same client to the same Stateless EJB, it should never be assumed that those consecutive calls will be handled by the same EJB instance.

Stateless EJB

@Stateless
public class StatelessEJB {

  public void doSomething(){

  }

}

EJB client

@EJB
private StatelessEJB statelessEJB;

public void clientMethod() {

  // These consecutive calls are NOT guaranteed to
  // be handled by the same Stateless EJB instance
  statelessEJB.doSomething();
  statelessEJB.doSomething();

}




About List