Spring @Transactional 注解:自调用 [英] Spring @Transactional Annotation : Self Invocation

查看:18
本文介绍了Spring @Transactional 注解:自调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当从同一个类内部调用事务方法时,它不会在事务中运行.Spring 为事务方法创建一个代理,并将它们包装在一个 try-catch 块中,并在发生异常时回滚.考虑以下场景:

I know when a transactional method is called from inside the same class it wouldn't be run in a transaction. Spring creates a proxy for transactional methods and wraps them in a try-catch block and rolls back if an exception occurs. Consider the following scenario:

@Transactional
public void saveAB(A a, B b)
{
    saveA(a);
    saveB(b);
}

@Transactional
public void saveA(A a)
{
    dao.saveA(a);
}

@Transactional
public void saveB(B b)
{
    dao.saveB(b);
}

假设从另一个对象调用saveAB,并且saveB发生异常,所以saveA成功完成但saveB> 没有.据我所知,即使 saveAsaveB 不是事务性的(因为它们是从同一个对象调用的),因为 saveAB 是事务性的,它仍然应该回滚.

Assume saveAB is called from another object and an exception occurred in saveB, so saveA completed successfully but saveB did not. To my knowledge even though saveA and saveB are not transactional (because they are called from the same object), since saveAB is transactional it should still roll back.

我不明白的是为什么人们说自调用会破坏事务?只要调用者方法是事务性的,难道一切都不应该按预期工作吗?有什么我在这里遗漏的吗?

What I don't understand is why do people say self invocation breaks transaction? As long as the caller method is transactional shouldn't everything work as expected? Is there anything I'm missing here?

推荐答案

我不明白的是为什么人们说自我调用中断交易?

What I don't understand is why do people say self invocation breaks transaction?

我从来没有听说过自我调用会破坏事务.我只知道自调用不会启动一个新的事务,你已经提到了原因.

I never heard that self-invocation breaks transaction. All I know is that self-invocation will not start a new transaction and you already mentioned the reason why.

Spring 事务管理规范的片段

注意 在代理模式下(默认),只有外部方法调用通过代理进入被拦截.这意味着自调用,实际上是目标对象中的一个方法调用目标对象的另一种方法,不会导致实际即使调用的方法被标记为@交易.

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.

<小时>

如果您从 saveAB() 中删除 @Transaction 注释,您将观察到该方法 saveA()saveB() 不会在事务下运行,即使它用 @Transactional 注释.但是,如果您从类外部调用 saveA()saveB(),它将按预期在事务下运行.这就是为什么人们建议对自我调用要谨慎的原因.


If you remove @Transaction annotation from saveAB(), you would observe that method saveA() and saveB() would not run under transaction even though it is annotated with @Transactional. However, if you call saveA() or saveB() from outside the class, it will run under transaction as expected. That is the reason why people advice to be cautious with self-invocation.

public void saveAB(A a, B b)
{
    saveA(a);
    saveB(b);
}

@Transactional
public void saveA(A a)
{
    dao.saveA(a);
}

@Transactional
public void saveB(B b)
{
    dao.saveB(b);
}

在我看来,自调用任何公共方法都是一个坏主意.

In my view, self-invoking any public method is a bad idea.

这篇关于Spring @Transactional 注解:自调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆