关于服务和@Transactional

发布于 2024-12-29 10:56:57 字数 144 浏览 5 评论 0原文

如果我有一个服务类连续调用其他三个服务类,并且每个子服务都必须在某个时刻处理一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫负雪 2025-01-05 10:56:57

Spring框架中默认的事务传播是 REQUIRED,这意味着如果交易尚不存在或代码加入现有交易,则创建该交易:

支持当前事务,如果不存在则创建一个新事务。类似于同名的 EJB 事务属性。

这是事务注释的默认设置。

这意味着,如果将对三个事务方法的调用包装在单个事务方法中,它们将全部在单个事务中运行。就这样。

另请参阅:

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:

如痴如狂 2025-01-05 10:56:57

如果您将外部服务注释为 @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.

口干舌燥 2025-01-05 10:56:57

这实际上是一个关于嵌套事务的问题(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文