2023-09-16  阅读(32)
原文作者:王伟王胖胖 原文地址: 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);
    		}
    	}

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

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


Java 面试宝典是大明哥全力打造的 Java 精品面试题,它是一份靠谱、强大、详细、经典的 Java 后端面试宝典。它不仅仅只是一道道面试题,而是一套完整的 Java 知识体系,一套你 Java 知识点的扫盲贴。

它的内容包括:

  • 大厂真题:Java 面试宝典里面的题目都是最近几年的高频的大厂面试真题。
  • 原创内容:Java 面试宝典内容全部都是大明哥原创,内容全面且通俗易懂,回答部分可以直接作为面试回答内容。
  • 持续更新:一次购买,永久有效。大明哥会持续更新 3+ 年,累计更新 1000+,宝典会不断迭代更新,保证最新、最全面。
  • 覆盖全面:本宝典累计更新 1000+,从 Java 入门到 Java 架构的高频面试题,实现 360° 全覆盖。
  • 不止面试:内容包含面试题解析、内容详解、知识扩展,它不仅仅只是一份面试题,更是一套完整的 Java 知识体系。
  • 宝典详情:https://www.yuque.com/chenssy/sike-java/xvlo920axlp7sf4k
  • 宝典总览:https://www.yuque.com/chenssy/sike-java/yogsehzntzgp4ly1
  • 宝典进展:https://www.yuque.com/chenssy/sike-java/en9ned7loo47z5aw

目前 Java 面试宝典累计更新 400+ 道,总字数 42w+。大明哥还在持续更新中,下图是大明哥在 2024-12 月份的更新情况:

想了解详情的小伙伴,扫描下面二维码加大明哥微信【daming091】咨询

同时,大明哥也整理一套目前市面最常见的热点面试题。微信搜[大明哥聊 Java]或扫描下方二维码关注大明哥的原创公众号[大明哥聊 Java] ,回复【面试题】 即可免费领取。

阅读全文