Mybatis中在关闭连接之前为什么要设置自动提交为true?

Posted kyuan1121

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis中在关闭连接之前为什么要设置自动提交为true?相关的知识,希望对你有一定的参考价值。

public void close() throws SQLException {
    if (connection != null) {
      resetAutoCommit();
      if (log.isDebugEnabled()) {
        log.debug("Closing JDBC Connection [" + connection + "]");
      }
      connection.close();
    }
  }

mybatis中的JdbcTransaction中的close方法, resetAutoCommit()方法是设置autoCommit=true。

源码:

 protected void resetAutoCommit() {
        try {
          if (!connection.getAutoCommit()) {
            // MyBatis does not call commit/rollback on a connection if just selects were performed.
            // Some databases start transactions with select statements
            // and they mandate a commit/rollback before closing the connection.
            // A workaround is setting the autocommit to true before closing the connection.
            // Sybase throws an exception here.
            if (log.isDebugEnabled()) {
              log.debug("Resetting autocommit to true on JDBC Connection [" + connection + "]");
            }
            connection.setAutoCommit(true);
          }
        } catch (SQLException e) {
          log.debug("Error resetting autocommit to true "
              + "before closing the connection.  Cause: " + e);
        }
      }

 

以上是关于Mybatis中在关闭连接之前为什么要设置自动提交为true?的主要内容,如果未能解决你的问题,请参考以下文章

在关闭pl/sql之前未提交的事物,会不会被默认提交?

mybatis源码分析 mybatis与spring事务管理分析

在postgresql插入大量数据,是不是先要关闭自动提交?

MyBatis核心配置文件概述

mybatis实现插入操作,框架默认事务自动提交是关闭的,为啥我没commit,数据就进库了?

Mybatis -- Mybatis相应API(SqlSessionFactoryBuilderSqlSessionFactory(可以设置自动提交事务)SqlSession)