简体   繁体   中英

Java, EJB, Concurrency lock for all methods

I have EJB with two methods

@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.

But I need more - if any method invoked by a thread, all other threads that call any method of EJB should wait. Does I have any way to do it?

The same question for @Stateless beans

@Lock(LockType.WRITE) locks ALL EJB methods of the bean, so it already does what you want.

Stateless beans only handle one client at a time, so concurrency should rarely be a problem there.

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. It is same as put @Lock(LockType.WRITE) on each method of the bean.

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. If no @Lock annotation is present on the singleton class, the default lock type, @Lock(WRITE) , is applied to all business and timeout methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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