zoukankan      html  css  js  c++  java
  • mybatis随笔二之SqlSessionFactory

    在上一篇文章我们已经得到了DefaultSqlSessionFactory

    @Override
      public SqlSession openSession() {
        return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
      }
    在configuration内部默认指定了protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
    private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
        Transaction tx = null;
        try {
          final Environment environment = configuration.getEnvironment();
          final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
          tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
          final Executor executor = configuration.newExecutor(tx, execType);
          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();
        }
      }
    从代码中我们可知开启了一个jdbc事务,并调用newExecutor得到一个执行期
    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;
      }
    如果开启了cache,那么会得到一个CachingExecutor,这采用的是装饰设计模式,它的语句执行实际是交给内部的delegate来执行的。
    里面值得注意的是使用了(Executor) interceptorChain.pluginAll(executor)将所有的拦截器注入进去形成一个拦截链。
    public Object pluginAll(Object target) {
        for (Interceptor interceptor : interceptors) {
          target = interceptor.plugin(target);
        }
        return target;
      }
    @Override
        public Object plugin(Object target) {
            return Plugin.wrap(target, this);
        }
    其实Plugin是mybatis的一个工具类,用来方便生成代理对象的,注意这个地方只会生成拦截目标是Executor的拦截类的代理对象,如下所示这种就不会调用。
    @Intercepts({
            @Signature(type = StatementHandler.class,
                    method = "prepare",
                    args = {Connection.class})
    })
    然后返回DefaultSqlSession的一个实例对象
    public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
        this.configuration = configuration;
        this.executor = executor;
        this.dirty = false;
        this.autoCommit = autoCommit;
      }

    至此DefaultSqlSessionFactory创建DefaultSqlSession的过程完成。


  • 相关阅读:
    系统提示MSVCR71.DLL缺失
    loadrunner11脚本录制乱码、控制台乱码
    性能测试之-脚本录制、web_url、web_submit_data
    tomcat启动报错排查
    环境配置问题
    python函数应用
    问题记录
    性能测试学习10_场景设计(二)
    性能测试学习09_场景设计(一)
    性能测试学习08_脚本编写总结
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/6394870.html
Copyright © 2011-2022 走看看