3
votes

I have a singleton EJB(3.1) which is using default container managed concurrency and default (LockType.Write) for all methods, since its not specified explicitly.

I have an Asynchronous method which looks as below:

@Asynchronous
@TransactionAttribute(value = TransactionAttributeType.NOT_SUPPORTED)
@AccessTimeout(-1)
public AsyncResult<Boolean> myAsyncMethod(....){

}

My question is, when invoked, will this method lock the whole singleton bean till the above method completes processing or will it lock only till the time the method returns the Future object to the client ?

Also what effect will "@AccessTimeout(-1)" the on the concurrency of this method/bean.

2

2 Answers

3
votes

My question is, when invoked, will this method lock the whole singleton bean till the above method completes processing or will it lock only till the time the method returns the Future object to the client ?

The singleton bean is never locked at all on the client thread. The client thread schedules the asynchronous method and returns a Future without accessing the bean. The thread that eventually executes the asynchronous method will lock the bean for the duration of the method execution.

Also what effect will "@AccessTimeout(-1)" the on the concurrency of this method/bean.

The @AccessTimeout(-1) will cause the thread that eventually attempts to execute the asynchronous method to block until the container-managed lock can be acquired.

1
votes

Answer to Q1: since the method is marked @Asynchronous, it will return immediately. No blocking what so ever

Answer to Q2: Using @AccessTimeout(-1) on a session bean method/class means that once a thread enters that method, it will block for ever until it completes processing. However, in this case its not a concern since the @Asynchronous will kick in, free up the calling thread and start the processing in a different background thread

Coincidentally, I happened to write a blog post on this topic couple of days back. I would also recommend reading up on the EJB 3.2 specification document - its a reliable and thorough reference material