2023-09-16  阅读(107)
原文作者:王伟王胖胖 原文地址: https://blog.csdn.net/wangwei19871103/article/details/105353778

处理提交流程图

202309162319479091.png

传播机制图

202309162319484532.png

AbstractPlatformTransactionManager的processCommit处理提交

其实这个跟处理回滚很像,先处理保存点,然后处理新事务,如果不是新事务不会真正提交,要等外层是新事务的才提交,最后根据条件执行数据清除,线程的私有资源解绑,重置连接自动提交,隔离级别,是否只读,释放连接,恢复挂起事务等。

    private void processCommit(DefaultTransactionStatus status) throws TransactionException {
    		try {
    			boolean beforeCompletionInvoked = false;
    
    			try {
    				boolean unexpectedRollback = false;
    				prepareForCommit(status);
    				triggerBeforeCommit(status);//提交前回调
    				triggerBeforeCompletion(status);//提交完成前回调
    				beforeCompletionInvoked = true;
    
    				if (status.hasSavepoint()) {//有保存点
    					if (status.isDebug()) {
    						logger.debug("Releasing transaction savepoint");
    					}
    					unexpectedRollback = status.isGlobalRollbackOnly();//是否有全局回滚标记
    					status.releaseHeldSavepoint();
    				}
    				else if (status.isNewTransaction()) {//当前状态是新事务
    					if (status.isDebug()) {
    						logger.debug("Initiating transaction commit");
    					}
    					unexpectedRollback = status.isGlobalRollbackOnly();
    					doCommit(status);//真正的提交
    				}
    				else if (isFailEarlyOnGlobalRollbackOnly()) {
    					unexpectedRollback = status.isGlobalRollbackOnly();
    				}
    
    				// Throw UnexpectedRollbackException if we have a global rollback-only
    				// marker but still didn't get a corresponding exception from commit.
    				if (unexpectedRollback) {//有全局回滚标记就报异常
    					throw new UnexpectedRollbackException(
    							"Transaction silently rolled back because it has been marked as rollback-only");
    				}
    			}
    			catch (UnexpectedRollbackException ex) {
    				// can only be caused by doCommit
    				triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);
    				throw ex;
    			}
    			catch (TransactionException ex) {
    				// can only be caused by doCommit
    				if (isRollbackOnCommitFailure()) {
    					doRollbackOnCommitException(status, ex);
    				}
    				else {
    					triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
    				}
    				throw ex;
    			}
    			catch (RuntimeException | Error ex) {
    				if (!beforeCompletionInvoked) {
    					triggerBeforeCompletion(status);
    				}
    				doRollbackOnCommitException(status, ex);
    				throw ex;
    			}
    
    			// Trigger afterCommit callbacks, with an exception thrown there
    			// propagated to callers but the transaction still considered as committed.
    			try {
    				triggerAfterCommit(status);//提交后回调
    			}
    			finally {//提交后清除线程私同步状态
    				triggerAfterCompletion(status, TransactionSynchronization.STATUS_COMMITTED);
    			}
    
    		}
    		finally {//根据条件,完成后数据清除,和线程的私有资源解绑,重置连接自动提交,隔离级别,是否只读,释放连接,恢复挂起事务等
    			cleanupAfterCompletion(status);
    		}
    	}

AbstractTransactionStatus的releaseHeldSavepoint释放保存点

如果没有异常回滚的话,要释放保存点。

    	public void releaseHeldSavepoint() throws TransactionException {
    		Object savepoint = getSavepoint();
    		if (savepoint == null) {
    			throw new TransactionUsageException(
    					"Cannot release savepoint - no savepoint associated with current transaction");
    		}
    		getSavepointManager().releaseSavepoint(savepoint);
    		setSavepoint(null);
    	}

JdbcTransactionObjectSupport的releaseSavepoint释放保存点

其实就是JDBC连接释放保存点。

    	@Override
    	public void releaseSavepoint(Object savepoint) throws TransactionException {
    		ConnectionHolder conHolder = getConnectionHolderForSavepoint();
    		try {
    			conHolder.getConnection().releaseSavepoint((Savepoint) savepoint);
    		}
    		catch (Throwable ex) {
    			logger.debug("Could not explicitly release JDBC savepoint", ex);
    		}
    	}

DataSourceTransactionManager的doCommit提交

获取JDBC的连接提交。

    @Override
    	protected void doCommit(DefaultTransactionStatus status) {
    		DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction();
    		Connection con = txObject.getConnectionHolder().getConnection();
    		if (status.isDebug()) {
    			logger.debug("Committing JDBC transaction on Connection [" + con + "]");
    		}
    		try {
    			con.commit();//JDBC连接提交
    		}
    		catch (SQLException ex) {
    			throw new TransactionSystemException("Could not commit JDBC transaction", ex);
    		}
    	}

大致的几个逻辑都讲完了,但是细节没讲,下次讲讲一些细节,现在对这个流程有大概了解就行。比如一些线程私有变量的作用,还有一些传播机制提交和回滚上的一些区别,顺便总结下。

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

阅读全文
  • 点赞