共计 1243 个字符,预计需要花费 4 分钟才能阅读完成。
接第 2 节内容,这是第 6 点具体解析
事务提交逻辑如下:
- 判断事务是否曾经实现,如果实现抛出异样
- 判断事务是否曾经被标记成回滚,则执行回滚操作
- 嵌入事务标记回滚,如果嵌入事务抛出了异样执行了回滚,然而在调用方把嵌入事务的异样个捕捉没有抛出,就会执行这一步。
- 提交事务
代码如下:
@Override
public final void commit(TransactionStatus status) throws TransactionException {
//1. 判断事务是不是曾经实现
if (status.isCompleted()) {
throw new IllegalTransactionStateException("Transaction is already completed - do not call commit or rollback more than once per transaction");
}
DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;
//2. 如果在事务链中曾经被标记回滚,那么不会尝试提交事务,间接回滚,不过我没找到在哪设置这个值
if (defStatus.isLocalRollbackOnly()) {if (defStatus.isDebug()) {logger.debug("Transactional code has requested rollback");
}
processRollback(defStatus);
return;
}
//3. shouldCommitOnGlobalRollbackOnly() 默认返回 false,isGlobalRollbackOnly 是在嵌入事务回滚的时候赋值的
if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {if (defStatus.isDebug()) {logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");
}
processRollback(defStatus);
// Throw UnexpectedRollbackException only at outermost transaction boundary
// or if explicitly asked to.
if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) {
throw new UnexpectedRollbackException("Transaction rolled back because it has been marked as rollback-only");
}
return;
}
//4. 提交事务
processCommit(defStatus);
}
正文完