简体   繁体   English

@Transactional(propagation = Propagation.REQUIRED)在春季?

[英]@Transactional(propagation = Propagation.REQUIRED) in spring?

If I have the following code : 如果我有以下代码:

@Component
public class A{ 

@Transactional(propagation = Propagation.REQUIRED)
public void a(){
    //logic
    b();
 //logic
}

@Transactional(propagation = Propagation.REQUIRED)
public void b(){
    //logic
} 
}

How many transactions open Spring in this code example? 在此代码示例中,有多少笔交易打开了Spring?

It doesn't matter. 没关系 When calling b() from a() it won't be going through the proxy, so any transactional attributes on b() won't be considered. 当从a()调用b() ,它将不会通过代理,因此不会考虑b()上的任何事务属性。

The example code has 1 transaction open if a() or b() is called through the proxy (ie outside of the class) and there isn't a transaction in progress already. 如果通过代理(即,在类外部b()调用a()b() ,并且尚无正在进行的事务,则示例代码将打开1个事务。

I add to @pablo answer the notice that in your example you cannot see the actual difference because your call your method within the same object which make @transaction behavior on the second method transparent with no effect : 我在@pablo回答中添加了一个通知,即在您的示例中您看不到实际的差异,因为您在同一对象内调用方法使第二个方法上的@transaction行为透明而没有效果:

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,也不会在运行时导致实际事务。

From spring documentation: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html 从Spring文档中: https : //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html

REQUIRED: Support a current transaction, create a new one if none exists 需要:支持当前交易,如果不存在则创建一个新交易

It only creates one transaction. 它只会创建一个交易。

Refering to the documentation Propagation.REQUIRED support a current transaction, create a new one if none exists. 请参阅文档 Propagation.REQUIRED支持当前事务,如果不存在则创建一个新事务。 The answer to your question is : 您的问题的答案是:

1 transaction, if there is no transaction when A#a() is called. 1个事务,如果在调用A#a()时没有事务。

0- zero if there is already one because it will be reused. 0-零(如果已经存在),因为它将被重用。

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

相关问题 @Transactional(传播= Propagation.REQUIRED) - @Transactional(propagation=Propagation.REQUIRED) 春季:传播。要求不起作用 - Spring:Propagation.REQUIRED not working Java Spring中的并发性:@Transactional(propagation = Propagation.REQUIRED,隔离= Isolation.SERIALIZABLE)不起作用 - Concurrency in java spring : @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE) not working @Transactional(readOnly = false,传播= Propagation.REQUIRED)引发异常 - @Transactional (readOnly = false, propagation = Propagation.REQUIRED) is throwing exception 如何从@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class) 中排除特定异常? - How To Exclude Specific Exception From @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)? 只要没有错误就可以更新/插入数据库(@Transactional(propagation = Propagation.REQUIRED)) - Just update/insert into DB when there are no errors (@Transactional(propagation = Propagation.REQUIRED)) “Propagation.REQUIRED”和“Propagation.REQUIRES_NEW”之间的区别? - Difference between "Propagation.REQUIRED" e "Propagation.REQUIRES_NEW"? Spring @事务隔离传播 - Spring @Transactional Isolation propagation Spring @Transactional传播属性 - Spring @Transactional propagation property Spring @Transactional - 隔离、传播 - Spring @Transactional - isolation, propagation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM