1. 生命周期
    MapperMethod 中execute方法需要传递一个sqlsession ,这个sqlsession属于SqlSessionTemplate类型,在SqlSessionTemplate生成中会初始化生成一个代理对象如图1,SqlSessionInterceptor中invoke方法负责sqlSession 的创建及销毁
    sqlsession
  private class SqlSessionInterceptor implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    //获取sqlsession,如果当前线程有事务则从事务管理中获取当前sqlsession,如果事务中未生成则生成一个sqlsession,
    //然后注册到 事务中。如果没有事务则生成sqlseesion                         
      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) {
        //关闭sqlsession
          closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
        }
      }
    }
  }
}

public static void closeSqlSession(SqlSession session, SqlSessionFactory sessionFactory) {
    notNull(session, NO_SQL_SESSION_SPECIFIED);
    notNull(sessionFactory, NO_SQL_SESSION_FACTORY_SPECIFIED);
    SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    //如果事务存在则交给事务决定是否关闭sqlsession
    if ((holder != null) && (holder.getSqlSession() == session)) {
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Releasing transactional SqlSession [" + session + "]");
      }
      holder.released();
    } else {
    //如果事务不存在,直接关闭sqlsession,如果事务存在则交给事务进行处理
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Closing non transactional SqlSession [" + session + "]");
      }
      session.close();
    }
  }
  1. 执行过程
    生成的sqlsession对参数进行处理后调用executor中的方法进行数据库操作

相关文章:

  • 2021-11-02
  • 2021-11-13
  • 2021-08-15
  • 2022-12-23
  • 2022-12-23
  • 2022-01-24
  • 2022-01-14
  • 2022-01-02
猜你喜欢
  • 2021-12-10
  • 2022-12-23
  • 2021-10-09
  • 2021-12-24
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案