博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis源码分析之SqlSession的创建过程
阅读量:7082 次
发布时间:2019-06-28

本文共 2786 字,大约阅读时间需要 9 分钟。

hot3.png

以上是之前的分析,在里分析到了事务管理器

SqlSession session = sqlSessionFactory.openSession();//DefaultSqlSessionFactory里的openSessionpublic 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();  }}

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

171053_YPoi_657390.png

public enum ExecutorType {  SIMPLE, REUSE, BATCH}

执行器类型只有三种

SIMPLE:普通的执行器;

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

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

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;}

二级缓存开关配置示例

  

执行器创建后

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 实例.

转载于:https://my.oschina.net/u/657390/blog/663991

你可能感兴趣的文章
NGINX反向代理
查看>>
MySQL集群---②Windows平台搭建MySQL CLUSTER集群
查看>>
完整部署CentOS7.2+OpenStack+kvm 云平台环境(6)--在线调整虚拟机的大小
查看>>
UEditor百度编辑器,工具栏自定义添加一个普通按钮
查看>>
HOLOLENS程序发布,这个界面调用的图片
查看>>
Atitit. Api 设计 原则 ---归一化
查看>>
JVM之数据类型
查看>>
【MongoDB】3.详细命令集合
查看>>
腾讯企业大学培训经验
查看>>
[LeetCode] Sort Characters By Frequency 根据字符出现频率排序
查看>>
python写的分析mysql binlog日志工具
查看>>
webapi - 使用依赖注入
查看>>
ubuntu 下非交互式执行远程shell命令
查看>>
centos7环境安装rabbitMQ
查看>>
时间换算方法
查看>>
JavaScript------正则表达式的使用
查看>>
python之参数解析模块argparse
查看>>
touch事件的分发和消费机制
查看>>
Codeforces Round #396 (Div. 2) C. Mahmoud and a Message dp
查看>>
activity 与 service 之间的通信
查看>>