PS:这篇博文承接上一篇:
MyBatis运行原理(一)SqlSessionFactory对象创建过程分析
在上一篇博文中分析了SqlSessionFactory
对象创建的过程,有了SqlSessionFactory
对象工厂就可以创建SqlSession
了,下面就来具体分析一下SqlSession
对象创建的过程。
一、SqlSession
对象创建过程分析
入口程序:
private SqlSessionFactory getSqlSessionFactory() throws IOException {String resource = "mybatis-config.xml";InputStream is = Resources.getResourceAsStream(resource);return new SqlSessionFactoryBuilder().build(is);}@Testpublic void testMyBatis3Simple() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();// 将断点打在下面代码的前面SqlSession sqlSession = sqlSessionFactory.openSession();}
1.首先会跳到DefaultSqlSessionFactory
类中的openSession()
方法中。
// ====== DefaultSqlSessionFactory 类中的方法 ======@Overridepublic SqlSession openSession() {return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);}
在上面这个方法中调用了另一个方法:
openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)
,这个方法做了些什么呢?下面我们就来追踪一下。
// ====== DefaultSqlSessionFactory 类中的方法 ======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);/*** ExecutorType 是一个枚举类型,默认是SIMPLE* 根据ExecutorType 创建一个Executor 对象* 因为Executor 对象比较重要,下面来分析一下Executor 对象创建的过程*/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();}}
2.configuration.newExecutor(tx, execType)
创建过程如下:
// ====== Configuration 类中的方法 ======public Executor newExecutor(Transaction transaction, ExecutorType executorType) {executorType = executorType == null ? defaultExecutorType : executorType;executorType = executorType == null ? ExecutorType.SIMPLE : executorType;Executor executor;/*** 根据executorType 类型创建对应的Executor * BatchExecutor:批量执行器* ReuseExecutor:会执行预处理的执行器* SimpleExecutor:简单的执行器*/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);}/*** 如果开启了二级缓存,则使用CachingExecutor 来包装executor,* 在查询之前都会先查询缓存中是否有对应的数据* 包装的过程使用了装饰者模式,装饰者模式可参考博文:* /codejas/article/details/79112824*/if (cacheEnabled) {executor = new CachingExecutor(executor);// 最后使用每个拦截器重新包装executor 并返回executor = (Executor) interceptorChain.pluginAll(executor);// executor 对象创建完成并返回return executor;}
3.Executor
对象创建完成后,会接着执行
openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)
方法。
// ====== DefaultSqlSessionFactory 类中的方法 ======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);/*** configuration 对象在创建SqlSessionFactory 对象的时候就已经创建了* Executor 对象创建完成后,使用executor与configuration 对象来创建DefaultSqlSession* 返回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();}}
到这里会接着向上一步返回,SqlSession
对象创建的过程也就结束了。
调用过程时序图:
二、总结
这篇博文对sqlSessionFactory
创建SqlSession
对象的过程进行了源码分析,最后返回的SqlSession
中包含有两个重要的对象,分别是configuration
与executor
。configuration
对象在创建SqlSessionFactory
的时候就已经被创建出来了,用来保存全局配置文件与SQL 映射文件中的信息,executor
是一个执行器对象。如果你想了解更多的细节,可以自己查看源码,希望这篇博文能够为你提供帮助。