3

The EJB 3.2 spec says the following:

By default, clients are allowed to make concurrent calls to a stateful session object and the container is required to serialize such concurrent requests. Note that the container never permits multi-threaded access to the actual stateful session bean instance.

To me concurrent access and multi-threaded access seem equivalent. So how is it possible to make concurrent calls to a stateful EJB while multiple threads are prohibited?

pepe
  • 814
  • 8
  • 12

2 Answers2

4

You are right: Concurrent calls can only be made with multiple threads. So, when speaking about concurrency, there are multiple threads involved.

The spec clearly says that the container is not allowed to concurrently access one stateful session bean instance. If there are concurrent calls (from multiple threads), the container must serialize them.

Note, that there could of course be several instances of that stateful session bean, which of course can be accessed by several clients.

Additionally - and that seems to confuse you - the spec say that a client (not the container) is allowed to make concurrent calls, but the container then must serialize them.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
  • Is this mean that I am allowed to safely inject a stateful EJB into a servlet with the @EJB annotation and multiple HTTP requests from multiple threads can make calls to the single shared stateful EJB, but a method of the SFSB will never run concurrently because the container will serialize the calls to the SFSB and if more calls arrive to the SFSB while the method is already running then I will get a ConcurrentAccessException? – pepe Apr 25 '14 at 08:31
  • You will not get an exception. The container will _serialize_ all incoming calls (that may come in concurrently) for a stateful session bean instance. There will be only one thread running one instance's method. – Seelenvirtuose Apr 25 '14 at 09:01
  • I see. By default all access attempts will wait forever to get an opportunity for running. Only if the AccessTimeout annotation is specified with higher value than -1 and the access attempt time outs I will get a ConcurrentAccessException. – pepe Apr 25 '14 at 09:10
1

A stateful session bean can process concurrent requests. However these calls do not actually access the bean class you've coded concurrently. Application server serializes these requests. So at a given time only a single thread executes the bean class methods.

So the thing is, concurrent call is not equal to concurrent access for stateful session beans.

This level of concurrency only handles requests of same session. Requests from different sessions are handled with different objects.

infiniteRefactor
  • 1,940
  • 15
  • 22