Using EJB to deal with concurrency
Concurrency management is one of the biggest advantages supplied by a Java EE server. You can rely on a ready environment to deal with this tricky topic.
This recipe will show you how you can set up your beans to use it!
Getting ready
Just add a Java EE dependency to your project:
<dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency>
How to do it...
The recipe will show you three scenarios.
In the first scenario, LockType
is defined at the class level:
@Singleton @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER) @Lock(LockType.READ) @AccessTimeout(value = 10000) public class UserClassLevelBean { private int userCount; public int getUserCount() { return userCount; } public void addUser(){ userCount++; } }
In...