简体   繁体   English

Spring @Transactional方法 - 参与交易

[英]Spring @Transactional method - participating transaction

in one dao I have 2 @Transactional methods. 在一个dao我有2个@Transactional方法。

if i do not provide any explicit properties, 如果我不提供任何明确的属性,

then what will happen, if 那么会发生什么,如果

I run one method in the body of another? 我在另一个体内运行一种方法?

Both methods will run within THE SAME ONE TRANSACTION? 这两种方法都在同一个交易中运行?

Proxies in Spring AOP Spring AOP中的代理

When using Transactional, you're dealing with proxies of classes, so in this scenario: 使用Transactional时,您正在处理类的代理,因此在这种情况下:

@Transactional
public void doSomeThing(){ // calling this method targets a proxy

    doSomeThingElse(); // this method targets the actual class, not the PROXY,
                       // so the transactional annotation has no effect
}

@Transactional
public void doSomeThingElse(){
}

you are calling the proxy from outside, but the second method call is made from inside the proxied object and therefor has no transactional support. 您从外部调用代理,但第二个方法调用是从代理对象内部进行的,因此没有事务支持。 So naturally, they run in the same transaction, no matter what the values of the @Transactional annotation in the second method are 很自然地,无论第二种方法中@Transactional注释的值是什么,它们都在同一个事务中运行

so if you need separate transactions, you have to call 所以如果你需要单独的交易,你必须打电话

yourservice.doSomething();
yourservice.doSomethingElse();

from outside. 从外面。

The whole scenario is explained pretty well in the chapter Spring AOP > Understanding AOP proxies , including this "solution": 整个场景在Spring AOP>理解AOP代理一章中得到了很好的解释,包括这个“解决方案”:

Accessing the Current AOP Proxy object from the inside 从内部访问当前AOP代理对象

public class SimplePojo implements Pojo {

   public void foo() {
      // this works, but... gah!
      ((Pojo) AopContext.currentProxy()).bar();
   }

   public void bar() {
      // some logic...
   }
}

The default value of the propagation attribute of @Transactional is REQUIRED , which means: @Transactionalpropagation属性的默认值是REQUIRED ,这意味着:

Support a current transaction, create a new one if none exists. 支持当前事务,如果不存在则创建新事务。

So yes - both methods will run in the same transaction. 所以是的 - 两种方法都将在同一个事务中运行。

But one important advice: don't make your DAO transactional . 但是一个重要的建议是: 不要让你的DAO交易 The services should be transactional, not the DAO. 服务应该是交易性的,而不是DAO。

Spring doc 春天的文件

one note: 一个说明:

In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. 在代理模式(默认设置)下,只拦截通过代理进入的外部方法调用。 This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional. 这意味着实际上,自调用目标对象中的一个方法调用目标对象的另一个方法,即使被调用的方法用@Transactional标记,也不会在运行时导致实际的事务。

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

相关问题 Spring @Transactional方法中没有事务启动 - No transaction starts within Spring @Transactional method Spring事务在@Transactional方法中处理JMSTemplate - Spring transaction handling JMSTemplate inside @Transactional method 通过反射调用@Transactional 方法时未创建 Spring 事务 - Spring transaction not being created when invoking @Transactional method via reflection Spring AOP没有从代理类中以@Transactional方法注入事务 - Spring AOP not injecting transaction in @Transactional method from a proxied class Spring事务不会在@Transactional方法中的RuntimeException回滚 - Spring Transaction doesn't Rollback at RuntimeException in @Transactional method Spring在@Transactional方法中捕获JpaSystemException并回滚事务 - Spring Catch JpaSystemException in @Transactional Method and Roll Back Transaction 在另一个实例中从事务方法调用 spring 非事务方法时,事务是否得到传播? - When calling a spring Non-Transactional method from a Transactional Method in another instance, does the transaction get propagated? Spring @Transactional 阻止打开事务 - Spring @Transactional prevent opening of Transaction Spring @Transactional如何“停止”交易 - Spring @Transactional how to “stop” transaction @Transactional Spring没有创建新事务 - @Transactional Spring not creating a new transaction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM