交易的传播行为
我正在为 spring aop 使用基于注释的声明性方法。 示例代码
ClassA{
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
add()
{
method1();
method2();
method3();
}
}
但我仍然对使用propagation.does propagation.Requires_New表示怀疑,意味着每个请求都会启动新事务。
第二个问题:
method2、method3等方法失败是否会导致事务回滚?
如果有人能帮助我学习交易传播,我将非常高兴。
有人可以给我提供一个现实世界的例子,我们需要参与现有的交易。因为我想象我们在上面的例子中使用的添加函数将独立于所有用户,或者任何其他函数将独立于每个调用的用户。我无法找到使用其他传播行为(如 PROPAGATION_SUPPORTS 、PROPAGATION_MANDATORY、PROPAGATION_REQUIRES_NEW 等)的示例
I am using annotation based declarative approach for spring aop.
sample code
ClassA{
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
add()
{
method1();
method2();
method3();
}
}
But I have still doubt on use of propagation.does propagation.Requires_New means that each request will start new transaction.
Second question:
Does failure of any method like method2,method3 will cause the transaction to rollback?
I will be very happy if any can help me to leans transaction propagation.
can someone provide me a real world example where we need a participate in existing transaction.because I visualise that add function that we are using in above example will be independent for all users,or any other function will be independent to each user who is calling. I am not able to find example where other propagation behaviour like PROPAGATION_SUPPORTS ,PROPAGATION_MANDATORY,PROPAGATION_REQUIRES_NEW etc. are used
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答这个评论,而不是实际的问题:
都不是。 request 和 session 都是特定于 Web 的范围,而 Spring Transaction 抽象与 Web 技术无关。
@Transactional
的范围是每个方法调用,因为@Transactional
是通过 Spring AOP 实现的。事务状态保存在 ThreadLocal 变量中,这些变量在进入最外层的 @Transactional 方法时初始化,并在离开时通过提交或回滚清除。整个抽象在 Java 方法级别上工作,因此不需要 Web 容器或从 Web 容器中获益。并在下面的评论中回答这个问题:
这是
传播
值与我的评论:不启动新事务,仅检查事务是否处于活动状态(必须位于另一个
@Transactional
方法调用或以编程方式创建的事务内)如果事务存在,则启动嵌套事务,否则启动新事务。
不启动事务。如果存在事务则失败。
不启动事务。暂停任何现有交易。
如果存在交易,则使用该交易,如果不存在,则创建一个新交易。 在 95% 的情况下,这就是您所需要的。
无论是否存在现有事务,始终创建新事务。如果有,它将在此方法执行期间暂停。
如果存在交易,则可以使用交易,但不需要交易(也不会启动新交易)。
在大多数情况下,
REQUIRED
就是您所需要的(因此它是REQUIRED
中的默认设置 ) code>@Transactional 注释)。我个人从未见过除了REQUIRED
和REQUIRES_NEW
之外的任何其他值。Answering this comment, not the actual question:
Neither. request and session are both web-specific scopes, while the Spring Transaction abstraction has nothing to do with web technologies.
The scope of
@Transactional
is per method invocation, as@Transactional
is implemented through Spring AOP. The transactional state is kept inThreadLocal
variables which are initialized when the outermost@Transactional
method is entered and cleared with commit or rollback when it is left. This whole abstraction works on Java method level, and hence does not require or profit from a web container.And in response to this Question in the comment below:
Here's the list of
Propagation
values with my comments:Does not start a new Transaction, just checks whether a transaction is active (must be inside either another
@Transactional
method call or a programmatically created transaction)Start a nested transaction if a transaction exists, start a new transaction otherwise.
Does not start a transaction. Fails if a transaction is present.
Does not start a transaction. Suspends any existing transaction.
If a transaction exists, use that, if not, create a new one. In 95% of cases, this is what you need.
Always creates a new transaction, no matter if an existing transaction is present. If there is, it will be suspended for the duration of this method execution.
Can use a transaction if one is present, but doesn't need one (and won't start a new one either)
In most cases,
REQUIRED
is what you need (hence it's the default in the@Transactional
annotation). I have personally never seen any other value butREQUIRED
andREQUIRES_NEW
in use.事务传播指示给定方法在调用时应该有什么行为。
REQUIRES_NEW
意味着即使有正在进行的事务,也应始终启动新事务。例如,如果
method1()
定义了REQUIRES_NEW
,那么它将在新事务中执行。异常会回滚当前活动事务,是的。
Transaction propagation indicates what should be the behaviour of the given method when it is invoked.
REQUIRES_NEW
means that a new transaction should always be started, even if there is an ongoing transaction.If
method1()
, for example, definesREQUIRES_NEW
, than it will execute in a new transaction.An exception will rollback the current active transaction, yes.
是的。 Requires_New意味着每个请求都会开始新的事务。并且method2、method3中的Yes失败将导致事务回滚,具体取决于回滚属性。
检查事务属性。
Yes. Requires_New means that each request will start new transaction. and Yes failure in method2,method3 will cause the transaction to rollback, depending on the rollback properties.
check Transactional properties.