@Transactional方法在没有@Transactional anotation的情况下调用另一个方法? [英] @Transactional method calling another method without @Transactional anotation?

查看:1099
本文介绍了@Transactional方法在没有@Transactional anotation的情况下调用另一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Service类中看到过一个标记为 @Transactional 的方法,但它也在同一个类中调用了一些未标记为 @Transactional

I've seen a method in a Service class that was marked as @Transactional, but it was also calling some other methods in that same class which were not marked as @Transactional.

这是否意味着对单独方法的调用导致应用程序打开与DB的单独连接或暂停父事务等?

Does it mean that the call to separate methods are causing the application to open separate connections to DB or suspend the parent transaction, etc?

没有任何注释的方法的默认行为是什么是由另一个方法调用 @Transactional 注释?

What's the default behavior for a method without any annotations which is called by another method with @Transactional annotation?

推荐答案

当你调用没有 @Transactional 在事务块中,父事务将继续使用新方法。它将使用父方法中的相同连接(使用 @Transactional )以及在被调用方法中导致的任何异常(不包含 @Transactional 将导致事务按照事务定义中的配置回滚。

When you call a method without @Transactional within a transaction block, the parent transaction will continue to the new method. It will use the same connection from the parent method(with @Transactional) and any exception caused in the called method(without @Transactional will cause the transaction to rollback as configured in the transaction definition.

如果使用 @Transactional 来自同一实例中 @Transactional 的方法的注释,然后被调用的方法事务行为不会对事务产生任何影响。但是如果你调用一个方法来自具有事务定义的另一个方法的事务定义,并且它们位于不同的实例中,然后被调用方法中的代码将遵循被调用方法中给出的事务定义。

If you call a method with a @Transactional annotation from a method with @Transactional within the same instance, then the called methods transactional behavior will not have any impact on the transaction. But if you call a method with a transaction definition from another method with a transaction definition, and they are in different instances, then the code in the called method will follow the transaction definitions given in the called method.

您可以在声明式事务管理部分找到更多详细信息。 /transaction.html>春天的交易文档

You can find more details in the section Declarative transaction management of spring transaction documentation.

Spring声明式事务模型使用AOP代理。所以AOP代理负责创建交易。只有当实例中的方法从实例外部调用时,AOP代理才会处于活动状态。

Spring declarative transaction model uses AOP proxy. so the AOP proxy is responsible for creation of the transactions. The AOP proxy will be active only if the methods with in the instance are called from out side the instance.

这篇关于@Transactional方法在没有@Transactional anotation的情况下调用另一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆