[英]UnexpectedRollBackException in Spring inner transaction
I have two classes: 我有两节课:
@Service
@Transaction
class A {
public void method1() {
private B;
try {
save1()
b.method2()
} catch (SqlException e) {
doSomeThing();
}
@Autowired
public setB(){
this.B = B;
}
}
}
@Service
class B {
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void method2(){
save2()
throw new SqlException();
}
}
I got an SqlException
as expected, but also an UnexpectedRollBackException
, and the program stops. 我按预期得到了一个SqlException
,但也是一个UnexpectedRollBackException
,程序停止了。 I want to know why the data persisted by save2() is not rolled back? 我想知道为什么save2()持久保存的数据没有回滚?
Is it a problem with outer transaction? 这是外部交易的问题吗?
UPDATE: I tried catching UnexpectedRollBackException
in class A and everything works fine. 更新:我尝试在A类中捕获UnexpectedRollBackException
,一切正常。 But I still need some kind of explanation why I get the exception? 但我仍然需要某种解释为什么我得到例外? I suppose the outer transaction should be suspended when the inner transaction begins, so why the rollback is unexpected for the outer transaction? 我想在内部事务开始时应该暂停外部事务,那么为什么外部事务的回滚是意外的呢?
Thanks. 谢谢。
First of all: you are not injecting the instance of B into class A via spring. 首先:你不是通过弹簧将B的实例注入A类。 ie your b is not managed by spring => this leads to the behaviour: Spring is ignoring the @Transactional
annotation on method. 即你的b不是由spring =>管理的,这会导致行为:Spring忽略了方法上的@Transactional
注释。
Second if you don't want to rollback on SqlException
you have to specify noRollbackFor=SqlException.class
其次,如果你不想回滚上SqlException
你必须指定noRollbackFor=SqlException.class
UPDATE: after clarification, the call is happening on managed bean. 更新:澄清后,调用发生在托管bean上。 But the problem that expected behaviour - transaction inside of transaction is not supported in general by the transaction management system out of the box. 但是,预期行为的问题 - 交易内部的交易通常不受交易管理系统的支持。 https://docs.spring.io/spring/docs/4.3.11.RELEASE/javadoc-api/org/springframework/transaction/annotation/Propagation.html#REQUIRES_NEW https://docs.spring.io/spring/docs/4.3.11.RELEASE/javadoc-api/org/springframework/transaction/annotation/Propagation.html#REQUIRES_NEW
Unless the details on that system are provided it is impossible to step forward. 除非提供有关该系统的详细信息,否则无法前进。 Out of content the NESTED transaction propagation could be better, then REQUIRES_NEW, because it is making a rollback point for the external transaction and starts new one. 在内容之外,NESTED事务传播可能会更好,然后REQUIRES_NEW,因为它正在为外部事务创建回滚点并启动新事务。
I have a simliar scenario like this and resolved it using propagation = Propagation.NESTED
from where the second method
is calling. 我有一个像这样的simliar场景,并使用propagation = Propagation.NESTED
从第二个method
调用的地方解决它。 rewrite the code as mentioned below and try. 重写下面提到的代码并尝试。
@Service
class A {
@Transactional(rollbackFor = { HibernateException.class}, propagation = Propagation.NESTED)
public void method1() {
private B;
try {
save1()
b.method2()
} catch (SqlException e) {
doSomeThing();
}
@Autowired
public setB(){
this.B = B;
}
}
}
Note: Transactional
is used in method level for class A
with propagation nested
. 注意: Transactional
用于class A
方法级别,并且propagation nested
了propagation nested
。 Class B
shown below. Class B
如下所示。
@Service
class B {
@Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
public void method2(){
save2()
throw new SqlException();
}
}
This has resolved the issue in my case. 这解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.