简体   繁体   English

Spring @Transactional传播属性

[英]Spring @Transactional propagation property

I have a question about spring transaction propagation. 我对春季交易传播有疑问。 I understand different Spring transaction propagation ie Propagation.REQUIRED , Propagation.REQUIRES_NEW etc. What I am not clear about is how would it behave in case of nested transactions with different propagation values. 我了解不同的Spring事务传播,即Propagation.REQUIREDPropagation.REQUIRES_NEW等。我不清楚的是在具有不同传播值的嵌套事务的情况下它将如何表现。

For example If I use @Transactional(propagation = Propagation.REQUIRED) to annotate a method m1 . 例如,如果我使用@Transactional(propagation = Propagation.REQUIRED)注释方法m1 If I call a method m2 which is annotated as REQUIRES_NEW . 如果我调用一个方法m2 ,它被注释为REQUIRES_NEW What would be the behaviour? 会是什么行为? If an exception occurs in m2 will it rollback the changes in m2 and as well as m1 or only the changes of m1 . 如果在发生异常m2将它回滚的变化m2和以及m1或仅变化m1 Similarly m1 annotated as REQUIRES_NEW and m2 as REQUIRED . 类似地, m1标注为REQUIRES_NEWm2标注为REQUIRED

It's explained pretty well in the spring documentation for Transaction Propagation . Spring的Transaction Propagation文档中对此进行了很好的解释。

The propagation behavior of a method defines how Spring behaves upon entry/exit of that method and what happens if a transaction already exists (or does not exist) at that time. 方法的传播行为定义了Spring在该方法进入/退出时的行为方式,以及当时某个事务已经存在(或不存在)的情况。

The propagation behaviors of methods up the stack (whatever is calling your method) are irrelevant. 方法在堆栈中的传播行为(无论调用什么方法)都是无关紧要的。

For your examples: 举个例子:

Outer (m1): REQUIRED 外部(m1): REQUIRED
Inner (m2): REQUIRES_NEW 内(m2): REQUIRES_NEW
Result: A completely separate transaction will be created for m2 which can commit/rollback separately. 结果:将为m2创建一个完全独立的事务,该事务可以分别提交/回滚。 m1 having REQUIRED is not relevant to m2. 具有REQUIRED m1与m2不相关。

Outer (m1): REQUIRES_NEW 外面(m1): REQUIRES_NEW
Inner (m2): REQUIRED 内(m2): REQUIRED
Result: Spring will check that a transaction exists upon entering m2 or create one if none exists. 结果:Spring在输入m2时将检查事务是否存在,如果不存在则创建一个事务。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM