java 关于spring事务传播的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2754160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Question about spring transaction propagation
提问by Just a learner
I have a question about spring transaction propagation. If I use @Transactional(propagation = Propagation.REQUIRED)to annotate a method m1. When execution logic enter m1, if there is already a transaction, m1 will use that one. When after m1, what about the transaction? It ends or still open?(if I call m1 in another method, and after the invocation there is still other things to do).
我有一个关于 spring 事务传播的问题。如果我使用@Transactional(propagation = Propagation.REQUIRED)来注释方法 m1。当执行逻辑进入m1时,如果已经有一个事务,m1会使用那个。当在 m1 之后,交易呢?它结束还是仍然打开?(如果我在另一个方法中调用 m1,并且在调用之后还有其他事情要做)。
In summary, I want to know when exiting an annotated method, the transaction ends or still open?
综上所述,我想知道当退出一个带注释的方法时,事务结束还是仍然打开?
Great thanks.
万分谢意。
回答by skaffman
Propagation.REQUIRED(documented here) will create a new transaction (if none exists for the current thread), or will join an existing transaction (if one exists).
Propagation.REQUIRED(documented here) 将创建一个新事务(如果当前线程不存在),或者将加入现有事务(如果存在)。
When the method exits, then the transaction will be completed (if entering the method caused a transaction to be created), or will leave the transaction open (if a transaction already existed when the method was entered). In other, words, it's symmetrical, and will leave the thread's transactional state in the same state it was before the method was entered.
当方法退出时,事务将完成(如果进入该方法导致创建了一个事务),或者将保持该事务打开(如果该事务在进入该方法时已经存在)。换句话说,它是对称的,并且会使线程的事务状态与进入方法之前的状态相同。

