mybatis源码分析之SqlSession的建立过程

mybatis之SqlSessionFactory
java

mybatis源码分析之Configuration
sql

mybatis源码分析之事务管理器
缓存

以上是以前的分析,在mybatis源码分析之事务管理器里分析到了事务管理器安全

SqlSession session = sqlSessionFactory.openSession();

//DefaultSqlSessionFactory里的openSession
public SqlSession openSession() {
  return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
  Transaction tx = null;
  try {
    //根据配置获取环境
    final Environment environment = configuration.getEnvironment();
    //构建事务工厂
    final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
    //经过事务工厂建立事务Transaction对象
    tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
    //建立执行器Executor对象
    final Executor executor = configuration.newExecutor(tx, execType);
    //根据configuration,executor建立DefaultSqlSession对象
    return new DefaultSqlSession(configuration, executor, autoCommit);
  } catch (Exception e) {
    closeTransaction(tx); // may have fetched a connection so lets call close()
    throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  } finally {
    ErrorContext.instance().reset();
  }
}

以前已经分析过事务管理器,下面分析执行器Executorsession

public enum ExecutorType {
  SIMPLE, REUSE, BATCH
}

执行器类型只有三种mybatis

SIMPLE:普通的执行器;源码分析

REUSE:执行器会重用预处理语句(prepared statements);fetch

BATCH:执行器将重用语句并执行批量更新。this

configuration.newExecutor(tx, execType);

//默认执行器类型
protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
//二级缓存的全局开关,默认开启缓存
protected boolean cacheEnabled = true;

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
  //executorType为null时executorType=ExecutorType.SIMPLE
  executorType = executorType == null ? defaultExecutorType : executorType;
  executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
  Executor executor;
  //根据执行器类型建立执行器
  if (ExecutorType.BATCH == executorType) {
    executor = new BatchExecutor(this, transaction);
  } else if (ExecutorType.REUSE == executorType) {
    executor = new ReuseExecutor(this, transaction);
  } else {
    executor = new SimpleExecutor(this, transaction);
  }
  //当cacheEnabled为true时建立CachingExecutor对象
  if (cacheEnabled) {
    executor = new CachingExecutor(executor);
  }
  executor = (Executor) interceptorChain.pluginAll(executor);
  return executor;
}

二级缓存开关配置示例spa

<settings>
  <setting name="cacheEnabled" value="true"/>
</settings>

执行器建立后

new DefaultSqlSession(configuration, executor, autoCommit);

//DefaultSqlSession构造方法
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
  this.configuration = configuration;
  this.executor = executor;
  this.dirty = false;
  this.autoCommit = autoCommit;
}

至此SqlSession建立完成,从以前的几篇和这篇能清晰的看到从读取配置文件到SqlSession建立的整个过程.须要注意的是SqlSession 的实例不是线程安全的,是不能被共享的,因此它的最佳的范围是请求或方法范围.每一个线程都应该有本身的 SqlSession 实例.

相关文章
相关标签/搜索