简体   繁体   English

Java,EJB,所有方法的并发锁

[英]Java, EJB, Concurrency lock for all methods

I have EJB with two methods 我有两种方法的EJB

@Singleton(name = "RatingCalculatorEJB")
public class RatingCalculatorBean {

    public void countRating() {}

    public void countRating(int someID) {}
}

By default all methods has concurrency lock: @Lock(LockType.WRITE) If method invoked by any thread - another thread will wait to invoke this method. 默认情况下,所有方法都具有并发锁:@ Lock(LockType.WRITE)如果任何线程调用方法 - 另一个线程将等待调用此方法。

But I need more - if any method invoked by a thread, all other threads that call any method of EJB should wait. 但是我需要更多 - 如果一个线程调用任何方法,那么调用EJB的任何方法的所有其他线程都应该等待。 Does I have any way to do it? 我有办法吗?

The same question for @Stateless beans @Stateless bean的相同问题

@Lock(LockType.WRITE) locks ALL EJB methods of the bean, so it already does what you want. @ Lock(LockType.WRITE)锁定bean的所有EJB方法,因此它已经完成了你想要的。

Stateless beans only handle one client at a time, so concurrency should rarely be a problem there. 无状态bean一次只处理一个客户端,因此并发应该很少成为问题。

You can try to make that method synchronized. 您可以尝试使该方法同步。 In that way only one thread can access the method at one time. 这样,一次只有一个线程可以访问该方法。

@Lock(LockType.WRITE) on bean won't lock all methods of the bean. bean上的@Lock(LockType.WRITE)不会锁定bean的所有方法。 It is same as put @Lock(LockType.WRITE) on each method of the bean. 它与bean的每个方法上放置@Lock(LockType.WRITE)相同。

Annotating a singleton class with @Lock specifies that all the business methods and any timeout methods of the singleton will use the specified lock type unless they explicitly set the lock type with a method-level @Lock annotation. 使用@Lock注释单例类指定单例的所有业务方法和任何超时方法都将使用指定的锁类型,除非它们使用方法级@Lock批注显式设置锁类型。 If no @Lock annotation is present on the singleton class, the default lock type, @Lock(WRITE) , is applied to all business and timeout methods. 如果单例类中不存在@Lock批注,则默认锁定类型@Lock(WRITE)将应用于所有业务和超时方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM