简体   繁体   English

Spring Singleton @Transactional并发访问

[英]Spring singleton @Transactional concurrent accesses

In a Spring 3 MVC web application scenario with @Controller , @Service, @Repository if I am annotating the @Service classes with @Transactional and leave the scope of bean to be a Singleton, would the transaction manager block the service method in case of concurrent accesses? 在使用@Controller @Service, @Repository @Controller@Service, @Repository的Spring 3 MVC Web应用程序场景中@Service, @Repository如果我使用@Transactional注释@Service类,并将bean的范围保留为Singleton,则事务管理器会在出现以下情况时阻塞该服务方法:并发访问?

In case of singleton, as there would be one instance of Service class in the application context at any point of time, a subsequent request by a different thread, to the same service method which is already executing inside a transaction need to wait till the service method completes processing, please let me know if this true. 在单例的情况下,由于在任何时间点在应用程序上下文中都会有一个Service类实例,因此不同线程对已在事务内部执行的同一服务方法的后续请求需要等待,直到该服务为止。方法已完成处理,如果是这样,请告诉我。

I have just started looking into Java EE 6 with @Stateless EJBs being used for service classes, and comparing it with how things are done in Spring. 我刚刚开始研究Java EE 6,并将@Stateless EJB用于服务类,并将其与Spring中的工作方式进行比较。

@Transactional specify that method code is executed in transaction. @Transactional指定在事务中执行方法代码。 There are different transaction isolation levels which specify the behavior of the method according to the underlying datastore (isolation property of @Transactional annotation). 有不同的事务隔离级别,它们根据基础数据存储区(@Transactional批注的隔离属性)指定方法的行为。

The are next isolation levels: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Isolation.html . 下一个隔离级别: http : //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Isolation.html

This isolation levels are referred to underlying data store, not to the method. 此隔离级别是指基础数据存储,而不是方法。

The most aggressive isolation level is serializable: A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented. 最积极的隔离级别是可序列化的:一个常量,指示防止脏读,不可重复读和幻像读。 This level includes the prohibitions in ISOLATION_REPEATABLE_READ and further prohibits the situation where one transaction reads all rows that satisfy a WHERE condition, a second transaction inserts a row that satisfies that WHERE condition, and the first transaction rereads for the same condition, retrieving the additional "phantom" row in the second read. 该级别包括ISOLATION_REPEATABLE_READ中的禁止条件,并且进一步禁止以下情况:一个事务读取满足WHERE条件的所有行,第二个事务插入满足WHERE条件的行,并且第一个事务针对相同条件重新读取,并检索其他“幻影”行中的第二读。 ( http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Isolation.html#SERIALIZABLE ) http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Isolation.html#SERIALIZABLE

If you want to limit access to this method maybe you should simply use synchronized keyword. 如果您想限制对此方法的访问,则应该只使用synced关键字。

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

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