- Jakarta EE Cookbook
- Elder Moraes
- 126字
- 2025-02-26 04:14:00
How to do it...
This recipe will take you through three scenarios. Let's get started:
- 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 the second scenario, LockType is defined at the method level:
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@AccessTimeout(value = 10000)
public class UserMethodLevelBean {
private int userCount;
@Lock(LockType.READ)
public int getUserCount(){
return userCount;
}
@Lock(LockType.WRITE)
public void addUser(){
userCount++;
}
}
- The third scenario is a self-managed bean:
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class UserSelfManagedBean {
private int userCount;
public int getUserCount() {
return userCount;
}
public synchronized void addUser(){
userCount++;
}
}
Now, let's see how this recipe works.