태그 보관물: sqlsession

mybatis의 SqlSession에서 commit();close(); vs close(); 어떤걸 해야 하나요?

코드를 보다보니, 아래와 같은 코드가 있어서 확인해 봤습니다.. ^^;;

try {

session = createSqlSession();
// db 처리.. .
} finally {
// db 세션 종료
if(session != null) {
session.commit();
session.close();
}

}

그래서, http://code.google.com/p/mybatis/source/browse/tags/mybatis-3.0.4/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java 의 session코드에서 commit메쏘드와 close메쏘드를 살펴 봤습니다..

* commit() : 오버로딩하는 두개의 메쏘드가 있네요.. ^^

  public void commit() {
    commit(false);
  }

  public void commit(boolean force) {
    try {
      executor.commit(isCommitOrRollbackRequired(force));
      dirty = false;
    } catch (Exception e) {
      throw ExceptionFactory.wrapException(“Error committing transaction.  Cause: ” + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

* close()

  public void close() {
    try {
      executor.close(isCommitOrRollbackRequired(false));
      dirty = false;
    } catch (Exception e) {
      throw ExceptionFactory.wrapException(“Error closing transaction.  Cause: ” + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

위 코드를 보시면, commit();close(); 코드와 close();가 있습니다..
코드를 따라가다 보면, 결국, http://code.google.com/p/mybatis/source/browse/tags/mybatis-3.0.4/src/main/java/org/apache/ibatis/transaction/jdbc/JdbcTransaction.java 에서, 아래와 같은 코드를 볼 수 있습니다.


  public void commit() throws SQLException {
    if (!connection.getAutoCommit()) {
      connection.commit();
    }
  }


  public void close() throws SQLException {
    resetAutoCommit();
    connection.close();
  }


  protected void resetAutoCommit() {
    try {
      if (!connection.getAutoCommit()) {
        // for compatibility we always use true, as some drivers don’t like being left in “false” mode.
        connection.setAutoCommit(true);
      }
    } catch (SQLException e) {
      // Only a very poorly implemented driver would fail here,
      // and there’s not much we can do about that.
      throw new TransactionException(“Error configuring AutoCommit.  ” +
          “Your driver may not support getAutoCommit() or setAutoCommit(). Cause: ” + e, e);
    }
  }

흠, 따라서 commit();close();와 close();가 동일할 거라는 심증만 가지게 되네요..^^;;
commit();close();해야 하나요?? close();만 하면 되까요?? ㅋㅋ