Search code examples
ejb

ConcurrentAccessTimeoutException in EJB singleton bean


Getting following exception while reading a method from EJB singleton bean. Even though 60 seconds time out set, exception shown 5000MILLISECONDS. How to increase this time limit?

Caused by: javax.ejb.ConcurrentAccessTimeoutException: WFLYEJB0241: EJB 3.1 PFD2 4.8.5.5.1 concurrent access timeout on TestBean- could not obtain lock within 5000MILLISECONDS

Here is timeout setting on bean:

@AccessTimeout(value = 60, unit = TimeUnit.SECONDS)
@Lock(LockType.READ)
public class TestBean {

Solution

  • The Singleton bean, as a definition, can only be instantiated once. It means that by default a @lock ("write") is set in the singleton each time that a method of it is called.

    All requests are gonna be serialized by the EJB container if a method of the bean is already called.

    The time starts running when the request is serialized. The @AccessTimeout is referred to this time. If an asynchronous request is not completed yet and the time that you set in the @AccessTimeout is already passed, the exception is thrown. Some EJB Containers use their own default value, and in your case, the EJB CONTAINER for wildfly is the one that appears in the error.

    So to solve your problem is going to depend on what you want to do, and in each case, there are a lot of possible solutions, but if you just want to avoid this time, you should use the annotation like this:

    @AccessTimeout (0), this means that the request cannot be serialized, and it must be executed immediately if the singleton is free, otherwise, it will be lost.

    My recommendation is to pass this concurrency handling to a methodlevel, so you can manipulate the bean easily.