今天高高兴兴上班,一打开电脑运行项目,突然就提示
org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
啥,什么操作,代码也没动呀,🤔那么,开始认真的分析,首先,然后 MethodA()-> MethodB(),MethodB 数据异常引起了业务异常,B 方法向上抛出了异常,在B()返回的时候,transaction被设置为rollback-only了,但是A()正常消化掉 try-catch 掉了,没有继续向外抛。那么A()结束的时候,transaction会执commit操作,但是transaction 中已经被设置为rollback-only了。
所以会出现这个错误。
看下源码
public final void commit(TransactionStatus status) throws TransactionException {
if (status.isCompleted()) {
throw new IllegalTransactionStateException("Transaction is already completed - do not call commit or rollback more than once per transaction");
} else {
DefaultTransactionStatus defStatus = (DefaultTransactionStatus)status;
if (defStatus.isLocalRollbackOnly()) {
if (defStatus.isDebug()) {
this.logger.debug("Transactional code has requested rollback");
}
this.processRollback(defStatus);
//会执行该判断
} else if (!this.shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {
if (defStatus.isDebug()) {
this.logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");
}
//执行回滚
this.processRollback(defStatus);
// 异常判定,也是本问题抛出异常的位子
if (status.isNewTransaction() || this.isFailEarlyOnGlobalRollbackOnly()) {
throw new UnexpectedRollbackException("Transaction rolled back because it has been marked as rollback-only");
}
} else {
this.processCommit(defStatus);
}
}
那么该如何解决呢?
因为我的 A()是一个只读方法,所以我重新配置了隔离机制 SUPPORTS
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
这个状态用一句话概括就是“有则加入事物,无也不创建事物”。对于只读的方法来说,其实不启用事务,也是没有关系的。
就跳过了在A() 结束Trasaction commit 的操作,也就避免了异常的抛出,当然还可以改成继续向上排除异常,在Controller 层处理。
注:自己手动抛出的异常,尽可能是RuntimeException 或 其子类,因为Spring的事务默认回滚是发生RuntimeException,
即使配置了rollbackFor = Exception.class
也不管用
回顾以下Spring的事务隔离级别:
MANDATORY Support a current transaction, throw an exception if none exists. |
NESTED Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else. |
NEVER Execute non-transactionally, throw an exception if a transaction exists. |
NOT_SUPPORTED Execute non-transactionally, suspend the current transaction if one exists. |
REQUIRED Support a current transaction, create a new one if none exists. |
REQUIRES_NEW Create a new transaction, suspend the current transaction if one exists. |
SUPPORTS Support a current transaction, execute non-transactionally if none exists. |