MyBatis-06-Spring的SqlSession和原始區別

YangDanMua發表於2024-04-16

DefaultSqlSession

這個就不說了,SQL 執行是呼叫執行器 Executor 執行

SqlSessionTemplate

建構函式,雖然沒有立即建立 SqlSession 傳入代理攔截器 SqlSessionInterceptor,但是攔截器是一個例項內部類,可以訪問到 SqlSessionFactory
並且 SqlSessionTemplate 不支援 commit、rollback、close 操作(UnsupportedOperationException)

public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
    PersistenceExceptionTranslator exceptionTranslator) {

  notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
  notNull(executorType, "Property 'executorType' is required");

  this.sqlSessionFactory = sqlSessionFactory;
  this.executorType = executorType;
  this.exceptionTranslator = exceptionTranslator;
  this.sqlSessionProxy = (SqlSession) newProxyInstance(SqlSessionFactory.class.getClassLoader(),
      new Class[] { SqlSession.class }, new SqlSessionInterceptor());
}

執行時,以 selectOne 舉例,是代理到 SqlSessionProxy 處理的,而 SqlSessionProxy 會先從 Spring 的事務上下文獲取 SqlSession,沒有獲取到就新建 SqlSession 並繫結到事務上下文
本質上還是使用 DefaultSqlSession 來處理,並且若是在 Spring 事務管理下,則執行過後不會提交

SqlSessionInterceptor

SqlSessionProxy 的攔截

private class SqlSessionInterceptor implements InvocationHandler {
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      SqlSession sqlSession = getSqlSession(SqlSessionTemplate.this.sqlSessionFactory,
        SqlSessionTemplate.this.executorType, SqlSessionTemplate.this.exceptionTranslator);
    try {
      Object result = method.invoke(sqlSession, args);
      if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {
        // force commit even on non-dirty sessions because some databases require
        // a commit/rollback before calling close()
        sqlSession.commit(true);
      }
      return result;
    } catch (Throwable t) {
      Throwable unwrapped = unwrapThrowable(t);
      if (SqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) {
        // release the connection to avoid a deadlock if the translator is no loaded. See issue #22
        closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
        sqlSession = null;
        Throwable translated = SqlSessionTemplate.this.exceptionTranslator
            .translateExceptionIfPossible((PersistenceException) unwrapped);
        if (translated != null) {
          unwrapped = translated;
        }
      }
      throw unwrapped;
    } finally {
      if (sqlSession != null) {
        closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
      }
    }
  }
}

相關文章