处理回滚流程图
传播机制图
AbstractPlatformTransactionManager的processRollback处理回滚
unexpectedRollback
这个一般是false
,除非是设置rollback-only=true
,才是true
,表示是全局的回滚标记。首先会进行回滚前回调,然后判断是否设置了保存点,比如NESTED
会设置,要先回滚到保存点。如果状态是新的事务,那就进行回滚,如果不是新的,就设置一个回滚标记,内部是设置连接持有器回滚标记。然后回滚完成回调,根据事务状态信息,完成后数据清除,和线程的私有资源解绑,重置连接自动提交,隔离级别,是否只读,释放连接,恢复挂起事务等
private void processRollback(DefaultTransactionStatus status, boolean unexpected) {
try {
boolean unexpectedRollback = unexpected;//意外的回滚
try {
triggerBeforeCompletion(status);//回滚完成前回调
//有保存点回滚到保存点
if (status.hasSavepoint()) {
if (status.isDebug()) {
logger.debug("Rolling back transaction to savepoint");
}
status.rollbackToHeldSavepoint();
}
else if (status.isNewTransaction()) {//当前状态是一个新事务
if (status.isDebug()) {
logger.debug("Initiating transaction rollback");
}
doRollback(status);//进行回滚
}
else {
// Participating in larger transaction
if (status.hasTransaction()) {
if (status.isLocalRollbackOnly() || isGlobalRollbackOnParticipationFailure()) {
if (status.isDebug()) {
logger.debug("Participating transaction failed - marking existing transaction as rollback-only");
}
doSetRollbackOnly(status);//设置连接要回滚标记,也就是全局回滚
}
else {
if (status.isDebug()) {
logger.debug("Participating transaction failed - letting transaction originator decide on rollback");
}
}
}
else {
logger.debug("Should roll back transaction but cannot - no transaction available");
}
// Unexpected rollback only matters here if we're asked to fail early
if (!isFailEarlyOnGlobalRollbackOnly()) {
unexpectedRollback = false;
}
}
}
catch (RuntimeException | Error ex) {
triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
throw ex;
}
//回滚完成后回调
triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);
// Raise UnexpectedRollbackException if we had a global rollback-only marker
if (unexpectedRollback) {
throw new UnexpectedRollbackException(
"Transaction rolled back because it has been marked as rollback-only");
}
}
finally {//根据事务状态信息,完成后数据清除,和线程的私有资源解绑,重置连接自动提交,隔离级别,是否只读,释放连接,恢复挂起事务等
cleanupAfterCompletion(status);
}
}
AbstractTransactionStatus的rollbackToHeldSavepoint回滚到保存点
如果设置了保存点的话,会进行处理,回滚到保存点,内部就是调用JDBC
的连接回滚,释放保存点,其实没做什么,空的,最后清除保存点:
public void rollbackToHeldSavepoint() throws TransactionException {
Object savepoint = getSavepoint();
if (savepoint == null) {
throw new TransactionUsageException(
"Cannot roll back to savepoint - no savepoint associated with current transaction");
}
getSavepointManager().rollbackToSavepoint(savepoint);//回滚到保存点
getSavepointManager().releaseSavepoint(savepoint);//释放保存点,其实啥都没做
setSavepoint(null);//清除保存点
}
JdbcTransactionObjectSupport的rollbackToSavepoint回滚到保存点
内部就是获取连接对象,然后调用rollback
回滚到保存点,然后重置连接持有器的回滚标记为false
。所以这里不会影响外层的提交,否则外层提交的时候发现有回滚标记,会进行回滚的,这个在讲提交的时候会说到。
@Override
public void rollbackToSavepoint(Object savepoint) throws TransactionException {
ConnectionHolder conHolder = getConnectionHolderForSavepoint();
try {
conHolder.getConnection().rollback((Savepoint) savepoint);//回滚到保存点
conHolder.resetRollbackOnly();//重置回滚标记,不需要回滚
}
catch (Throwable ex) {
throw new TransactionSystemException("Could not roll back to JDBC savepoint", ex);
}
}
重置回滚标记:
DataSourceTransactionManager的doRollback回滚
真正的回滚就是这里啦,也是获取到JDBC
的连接,然后回滚。
@Override
protected void doRollback(DefaultTransactionStatus status) {
DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction();
Connection con = txObject.getConnectionHolder().getConnection();
if (status.isDebug()) {
logger.debug("Rolling back JDBC transaction on Connection [" + con + "]");
}
try {
con.rollback();//jdbc的回滚
}
catch (SQLException ex) {
throw new TransactionSystemException("Could not roll back JDBC transaction", ex);
}
}
DataSourceTransactionManager的doSetRollbackOnly设置回滚标记
如果既没有保存点,又不是新的事务,如果可以设置全局的回滚标记的话,就会设置。
@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction();
if (status.isDebug()) {
logger.debug("Setting JDBC transaction [" + txObject.getConnectionHolder().getConnection() +
"] rollback-only");
}
txObject.setRollbackOnly();
}
DataSourceTransactionObject 的setRollbackOnly设置回滚标记
最终就是设置了连接持有器的回滚标记啦。
public void setRollbackOnly() {
getConnectionHolder().setRollbackOnly();
}
最后再进行cleanupAfterCompletion
的清除处理,里面也涉及到很多东西,后面说。
好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。