EJB @Lock

The previous part showed that EJB @Asynchronous and @Schedule can be replaced with the new EE concurrency utilities in some measure. However, there are still some EJB APIs that are not easy to replace without coding them yourself. The @Lock API is one of them.

The global idea is to ensure that the data owned by the EJB (@Singleton) is accessed in a thread-safe context.

Indeed, this API is limited to the singleton EJB, since without a single instance usable in a concurrent environment, it doesn't make sense to lock.

The usage is straightforward, as you just decorate a method or the bean with @Lock, passing READ or WRITE in the parameter, depending on the kind of access:

@Singletonpublic class MyLocalData {    private Data data; @Lock(READ) ...

Get Java EE 8 High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.