关于服务和@Transactional
如果我有一个服务类连续调用其他三个服务类,并且每个子服务都必须在某个时刻处理一个 DAO 对象,那么我该如何使包装器服务将它们全部包装到一个事务中?它会像用 @Transactional 注解包装器一样简单吗?如果 DAO 已标记为 @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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Spring框架中默认的事务传播是
REQUIRED
,这意味着如果交易尚不存在或代码加入现有交易,则创建该交易:这意味着,如果将对三个事务方法的调用包装在单个事务方法中,它们将全部在单个事务中运行。就这样。
另请参阅:
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: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 并由服务调用,那么它们将默认加入您希望的外部事务。
If you annotate the outer service as @Transactional and your DAOs are also @Transactional and called by the service they will by default join the outer transaction as you're hoping.
这实际上是一个关于嵌套事务的问题(http://en.wikipedia.org/wiki/Nested_transaction)。对于 spring,(假设您使用的是版本 3 和注释),REQUIRED 是事务模式的默认值。如果您为服务方法设置此模型,则“包装器”服务包装的所有方法都将使用主机事务,这意味着它们将在同一事务中运行。
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.