简体   繁体   中英

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? Will it be as simple as annotating the wrapper with @Transactional? What if the DAO is already marked as @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:

Support a current transaction, create a new one if none exists. Analogous to EJB transaction attribute of the same name.

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). with spring, (assume you are using version 3 and annotation), REQUIRED is default for transaction mode. 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.

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