Search code examples
concurrencylockingsingletonejb

Will Singleton Session Bean invocation order be fair when locked?


When working with @Singleton type of EJB having container managed concurrency enabled and there is a pending write lock to @Lock(LockType.WRITE) annotated method, will possible callers of @Lock(LockType.READ) methods be queued in order of invocations?

In other words, if multiple invocations to read locked methods are pending a write locked invoker, will those read locked method callers get their calls through in order in which the invocations arrived (assuming no timeouts occur)?

I've been testing this and have gotten somewhat conflicting results.


Solution

  • The EJB specification makes no guarantees about singleton session bean lock fairness, so you shouldn't make any assumptions. If your application server makes some guarantee, I guess you could rely on that, but you're probably better off using @ConcurrencyManagement(BEAN) and using your own lock that ensures fairness.