关于mybatis的执行过程的简单分析(二)

第二个过程sqlSessionFactory.openSession()

第一个步骤,获得sqlsessionFactory简单分析完之后,现在开始接着分析.sqlSessionFactory.openSession()

现在开始记录:debug进入sqlSessionFactory.openSession();方法体里面,代码如图所示:

  @Override
  public SqlSession openSession() {
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
  }

configuration.getDefaultExecutorType() 获得的DefaultExecutorType默认执行器的类型,官网文档上有介绍:
在这里插入图片描述
默认是SIMPLE,
debug当中显示的信息如下:
在这里插入图片描述
代码#1接着看: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);
      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();
    }
  }

主要的步骤是获取一些信息,如environment ,创建事务tx,然后执行了一条语句Executor executor = configuration.newExecutor(tx, execType);这个是myabtis架构图中的一个重要组件Excutor,(执行器);
重点看这个executor是如何new出来的,

  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) {//#7
      executor = new CachingExecutor(executor);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

根据executorType,执行对应的new操作,我这里是:executor = new SimpleExecutor(this, transaction);
然后接着往下执行#7处,if (cacheEnabled),判断是否开启二级缓存,如果开启了二级缓存的话,用CachingExecutor这个类包装一下,生成一个被包装过的executor.,(被包装过的executor,在实行查询操作时的时候会先从缓存当中,查找。截图如下:)
在这里插入图片描述
代码接着往下走,执行到executor = (Executor) interceptorChain.pluginAll(executor);,执行拦截器链的的pluginAll()方法,进去看一下这个方法,代码如下:

  public Object pluginAll(Object target) {
    for (Interceptor interceptor : interceptors) {
      target = interceptor.plugin(target);
    }
    return target;
  }

这段代码的意思比较好理解,就是拿到所有的拦截器,执行每一个拦截器的plugin(target)方法,这里的target就是我们传入的executor,就是对exexutor进行重新包装,这里应该是自定义myatis插件的时候,这个方法会起作用,我这里没有自定义拦截器。
调用这个方法之后,我们的executor对象就返回啦。
然后返到openSessionFromDataSource()方法里(代码#1),然后执行new DefaultSqlSession(configuration, executor, autoCommit);
这个就是我们执行opeanSession();所获得的SqlSession.。

为了加强自己的记忆,简单画了一下openSession()方法过程的时序图:

在这里插入图片描述

小结:openSession()的大概步骤如下:
1.执行openSessionFromDataSource()方法
1.1获得基本配置信息,创建事务
1.2创建executor执行器,如果开启二级缓存,包装成带有缓存的executor
1.3调用拦截器链,包装executor
根据返回的executor 执行new DefaultSqlSession(configuration, executor, autoCommit)方法
返回DefaultSqlSession 就是我们需要的SqlSessionFactory

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值