SqlSessionTemplate

mybatis中SqlSessionTemplate实现了SqlSession接口

如下面源码中,这里使用SqlSessionInterceptor这个内部类,来生成了一个SqlSession的代理对象:

public class SqlSessionTemplate implements SqlSession {  private final SqlSessionFactory sqlSessionFactory;  private final ExecutorType executorType;  private final SqlSession sqlSessionProxy;  private final PersistenceExceptionTranslator exceptionTranslator;  /**   * Constructs a Spring managed SqlSession with the {@code SqlSessionFactory}   * provided as an argument.   *   * @param sqlSessionFactory   */  public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {    this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());  }  /**   * Constructs a Spring managed SqlSession with the {@code SqlSessionFactory}   * provided as an argument and the given {@code ExecutorType}   * {@code ExecutorType} cannot be changed once the {@code SqlSessionTemplate}   * is constructed.   *   * @param sqlSessionFactory   * @param executorType   */  public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {    this(sqlSessionFactory, executorType,        new MyBatisExceptionTranslator(            sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true));  }  /**   * Constructs a Spring managed {@code SqlSession} with the given   * {@code SqlSessionFactory} and {@code ExecutorType}.   * A custom {@code SQLExceptionTranslator} can be provided as an   * argument so any {@code PersistenceException} thrown by MyBatis   * can be custom translated to a {@code RuntimeException}   * The {@code SQLExceptionTranslator} can also be null and thus no   * exception translation will be done and MyBatis exceptions will be   * thrown   *   * @param sqlSessionFactory   * @param executorType   * @param exceptionTranslator   */  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());  }复制代码

SqlSessionInterceptor:

private class SqlSessionInterceptor implements InvocationHandler {    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);        }      }    }  }复制代码

如上,在Interceptor中定义了sqlSession的commit,close



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值