官术网_书友最值得收藏!

  • Jakarta EE Cookbook
  • Elder Moraes
  • 231字
  • 2021-06-24 16:12:47

How it works...

The first thing to have a look at is the following:

@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)

This is completely redundant! Singleton beans are container-managed by default, so you don't need to specify them.

Singletons are designed for concurrent access, so they are the perfect use case for this recipe.

Now, let's check the LockType defined at the class level:

@Lock(LockType.READ)
@AccessTimeout(value = 10000)
public class UserClassLevelBean {
...
}

When we use the @Lock annotation at the class level, the informed LockType will be used for all class methods.

In this case, LockType.READ means that many clients can access a resource at the same time. This is usually used for reading data.

In the case of some kind of locking, LockType will use the @AccessTimeout annotation time defined to run into a timeout or not.

Now, let's check the LockType defined at the method level:

@Lock(LockType.READ)
public int getUserCount(){
return userCount;
}

@Lock(LockType.WRITE)
public void addUser(){
userCount++;
}

Here, we are basically saying that getUserCount() can be accessed by many users at once (LockType.READ), but addUser() will be accessed just by one user at a time (LockType.WRITE).

The last case is the self-managed bean:

@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class UserSelfManagedBean{

...

public synchronized void addUser(){
userCount++;
}

...
}

In this case, you have to manage all the concurrency issues for your bean in your code. We used the synchronized qualifier as an example.

主站蜘蛛池模板: 天柱县| 墨竹工卡县| 黔西| 永吉县| 沿河| 南宁市| 临桂县| 河津市| 荃湾区| 东丽区| 武强县| 东台市| 壶关县| 津南区| 诏安县| 三江| 贵德县| 合川市| 娱乐| 忻城县| 诸暨市| 兴和县| 黄梅县| 闻喜县| 江都市| 鹿邑县| 申扎县| 池州市| 长海县| 密云县| 新田县| 舞钢市| 铁力市| 达拉特旗| 西平县| 峨山| 西安市| 绵阳市| 神农架林区| 全南县| 定州市|