`
blue2048
  • 浏览: 178167 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Mybatis深入剖析 -Executor初始化分析

阅读更多

Executor的初始化

  •     跟随SqlSession,Executory是SqlSession的对象属性
  •     依赖-Transaction,执行时需要根据特定的事务,进行commit,rollback,close操作
  •     Executory的创建过程如下
  1.     在Configuration的配置文件中执行全局的ExecutoryType,有BATCH,REUSE,SIMPLE,如果Cache开启的话还有CachingExecutor,他是前三种Executor的代理类
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    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);
    }
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

 

   2.     在SqlSessionFactory创建SqlSession时,根据configuration创建Executor

 private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Connection connection = null;
    try {
      final Environment environment = configuration.getEnvironment();
      final DataSource dataSource = getDataSourceFromEnvironment(environment);
      TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      connection = dataSource.getConnection();
      if (level != null) {
        connection.setTransactionIsolation(level.getLevel());
      }
      connection = wrapConnection(connection);
      Transaction tx = transactionFactory.newTransaction(connection, autoCommit);
      Executor executor = configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
      closeConnection(connection);
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

 

   3.    各种Executor的分析

  • SimpleExecutor-简单的Executor,所有的数据库操作均委托给StatementHandler处理,代码如下
 Configuration configuration = ms.getConfiguration();
 StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null);
 stmt = prepareStatement(handler);
 return handler.update(stmt);
 
  • BatchExecutor - 批量更新的Executor,使用jdbc的batchupdate方法,使用此Executor,需要手动执行SqlSession的flushStatements,真正出发sql的执行
  • ReuseExecutor-大体上与SimpleExecutor相同,只是根据sql缓存了jdbc的statement,遇到相同的sql时,省去了statement的创建,提高效率,但需要手动执行SqlSession的flushStatements,清除缓存
  • CachingExecutor - 在mapper中配置cache,对查询结果做缓存,其中缓存的key根据以下代码存数生成,建议最好不要用mybatis这个层次的缓存,一般业务层的缓存更实用,而且更容易控制
public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds) 
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics