Concurrency
Having a good understanding of the Concurrency semantics of different sessions beans is extremly important in order to be able to leverage them effectively. This chapter covers concurrency related annotations and talks about how they affect performance characteristics
Specifies the time period after which a queued request (waiting for another thread to complete) times out. This feature does not make sense for Stateless beans since the container allocates a new bean instance for each request.
This annotation is valuable for Stateful beans as well as Singleton beans configured with container managed concurrency option (
ConcurrencyManagementType.CONTAINER
)
During peak loads, your session bean instances are bombarded with concurrent requests. The EJB container ensures sanity by serializing these calls i.e. blocking other threads until the current thread finishes execution. Think of this annotation as a concurrency policy. It ensures that the potential (waiting) threads are not kept blocked forever.
You can use the value and unit elements of this annotation to refine its behaviour
Here are a few usage options
@AccessTimeout(0)
– this means that your method does not support concurrent access at all and the client would end up getting ajavax.ejb.ConcurrentAccessException
@AccessTimeout(-1)
– your method will block indefinitely@AccessTimeout(5000)
– method will wait for 5000 ms (5 seconds) before the next thread in queue (if any) if given a chance
@ConcurrencyManagement
@ConcurrencyManagement
A Singleton bean is flexible in terms of Concurrency Management - by default (convention), the container handles concurrent access to a Singleton bean. By using the @ConcurrencyManagement
annotation, you as a bean developer can take the bean concurrency management into your own control.
The possible values (enums) for this annotation are - ConcurrencyManagementType.CONTAINER
and ConcurrencyManagementType.BEAN
. The nomenclature of these enums clearly define their role.
In case you opt for Bean managed concurrency, you as a bean developer are responsible for the thread safety of objects used within the Singleton bean e.g. using synchronized methods, thread safe collections etc
@Lock
@Lock
When using Container managed concurrency (ConcurrencyManagementType.CONTAINER
) with Singleton EJBs, the @Lock annotation can help define whether or not you want allow concurrent access to your bean methods. If this annotation is not used, the default value is LockType.WRITE
(enum) which signifies that the method cannot be accessed by multiple threads at the same time. The other option LockType.READ
, behaves in exactly the opposite manner i.e. it allows multiple threads to concurrently access this method given that there is no thread which is currently holding a write lock on the Singleton bean instance.
What's next ?
Let's explore the Aspect Oriented features provided by EJBs !
Last updated