简体   繁体   English

关于服务和@Transactional

[英]On services and @Transactional

If I have a service class which calls three other service classes in a row, and each of those sub-services has to deal with a DAO object at some point, how can I make so that the wrapper service wraps them all into a single transaction? 如果我有一个连续调用三个其他服务类的服务类,并且每个子服务必须在某个时刻处理DAO对象,我该如何使包装服务将它们全部包装到一个事务中? Will it be as simple as annotating the wrapper with @Transactional? 它会像使用@Transactional注释包装一样简单吗? What if the DAO is already marked as @Transactional? 如果DAO已经标记为@Transactional怎么办?

The default transaction propagation in Spring framework is REQUIRED , which means that the transaction is created if it does not already exist or the code joins existing one: Spring框架中的默认事务传播是REQUIRED ,这意味着如果事务尚不存在或代码加入现有事务,则创建事务:

Support a current transaction, create a new one if none exists. 支持当前事务,如果不存在则创建新事务。 Analogous to EJB transaction attribute of the same name. 类似于同名的EJB事务属性。

This is the default setting of a transaction annotation. 这是事务注释的默认设置。

This means that if you wrap calls to three transactional methods in a single transactional method, they will all run within a single transaction. 这意味着如果在单个事务方法中包装对三个事务方法的调用,它们将在单个事务中运行。 Just like that. 就像那样。

See also: 也可以看看:

如果您将外部服务注释为@Transactional并且您的DAO也是@Transactional并且由服务调用它们将默认加入外部事务,如您所希望的那样。

this is actually a question about nested transaction (http://en.wikipedia.org/wiki/Nested_transaction). 这实际上是关于嵌套事务的问题(http://en.wikipedia.org/wiki/Nested_transaction)。 with spring, (assume you are using version 3 and annotation), REQUIRED is default for transaction mode. 使用spring,(假设您使用的是版本3和注释),REQUIRED是事务模式的默认值。 If you set this model for your service methods, all methods wrapped by your "wrapper" service will use the host transaction, which means they will run in same transaction. 如果为服务方法设置此模型,则“包装器”服务包装的所有方法都将使用主机事务,这意味着它们将在同一事务中运行。

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

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