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);
        }
      }

猜你喜欢

转载自www.cnblogs.com/kyuan1121/p/11827037.html