美文网首页
Spring事务模板TransactionTemplate

Spring事务模板TransactionTemplate

作者: Ylm007 | 来源:发表于2019-04-04 17:50 被阅读0次

    1、整体流程

    • 从Datasource获取conn
    • 设置为非自动提交(这就相当于开启事务了)
    • 缓存当前链接
    • 开始执行业务SQL:先从当前线程缓存获取conn
    • 判断是否自动提交
    • 执行sql
    • 模版commit
    • 模版清除线程的conn
    • 模版reset conn为自动提交
      //获取链接,自动事务提交设为false,预示接下来的操作是一个事务,缓存当前connectionHolder到当前线程
                TransactionStatus status = this.transactionManager.getTransaction(this);
                T result;
                try {
    //执行sql,尝试从当前线程获取conn,并检查事务提交方式
                    result = action.doInTransaction(status);
                }
                catch (RuntimeException ex) {
                    // Transactional code threw application exception -> rollback
                    rollbackOnException(status, ex);
                    throw ex;
                }
                catch (Error err) {
                    // Transactional code threw error -> rollback
                    rollbackOnException(status, err);
                    throw err;
                }
                catch (Throwable ex) {
                    // Transactional code threw unexpected exception -> rollback
                    rollbackOnException(status, ex);
                    throw new UndeclaredThrowableException(ex, "TransactionCallback threw undeclared checked exception");
                }
    //提交事务,恢复链接的为自动提交,清空当前线程的conn
                this.transactionManager.commit(status);
    

    2、开启事务操作

    if (txObject.getConnectionHolder() == null ||
                        txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
                    Connection newCon = this.dataSource.getConnection();
                    if (logger.isDebugEnabled()) {
                        logger.debug("Acquired Connection [" + newCon + "] for JDBC transaction");
                    }
    //从链接池拿链接过程
                    txObject.setConnectionHolder(new ConnectionHolder(newCon), true);
                }
    
        txObject.getConnectionHolder().setSynchronizedWithTransaction(true);
                con = txObject.getConnectionHolder().getConnection();
    
                Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
                txObject.setPreviousIsolationLevel(previousIsolationLevel);
    
                // Switch to manual commit if necessary. This is very expensive in some JDBC drivers,
                // so we don't want to do it unnecessarily (for example if we've explicitly
                // configured the connection pool to set it already).
                if (con.getAutoCommit()) {
                    txObject.setMustRestoreAutoCommit(true);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Switching JDBC Connection [" + con + "] to manual commit");
                    }
    //此处设置为非自动提交
                    con.setAutoCommit(false);
                }
    

    3、业务SQL执行时通过当前线程缓存获取conn

    ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
            if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {
                conHolder.requested();
                if (!conHolder.hasConnection()) {
                    logger.debug("Fetching resumed JDBC Connection from DataSource");
                    conHolder.setConnection(dataSource.getConnection());
                }
                return conHolder.getConnection();
            }
    

    4结束事务操作的清理工作

    DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
    
            // Remove the connection holder from the thread, if exposed.
            if (txObject.isNewConnectionHolder()) {
                TransactionSynchronizationManager.unbindResource(this.dataSource);
            }
    
            // Reset connection. 重新设置为自动提交
            Connection con = txObject.getConnectionHolder().getConnection();
            try {
                if (txObject.isMustRestoreAutoCommit()) {
                    con.setAutoCommit(true);
                }
                DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel());
            }
    
    image.png

    相关文章

      网友评论

          本文标题:Spring事务模板TransactionTemplate

          本文链接:https://www.haomeiwen.com/subject/gkmqiqtx.html