博客
关于我
2021-04-11 MyBatis运行流程-02
阅读量:169 次
发布时间:2019-02-28

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

获取SqlSession对象的实现分析与源码解读

一、获取SqlSession对象

SqlSession对象是数据库操作的核心单元,获取过程涉及多个关键步骤。以下是具体实现方法:

1.openSession()

通过调用sqlSessionFactory.openSession()方法获取SqlSession实例:

SqlSession session = sqlSessionFactory.openSession();

2.openSessionFromDataSource()

根据全局配置获取默认的Executor类型,创建事务上下文:

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

3.创建事务管理器

获取环境信息,创建事务工厂并初始化默认事务:

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {    Transaction tx = null;    try {        // 获取环境信息        final Environment environment = configuration.getEnvironment();        // 创建事务        tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);        // 创建执行器        final Executor executor = configuration.newExecutor(tx, execType);        // 创建SqlSession        return new DefaultSqlSession(configuration, executor, autoCommit);    } catch (Exception e) {        closeTransaction(tx);        throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);    } finally {        ErrorContext.instance().reset();    }}

4.newExecutor()

根据ExecutorType创建相应的执行器实例:

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

5.二级缓存配置

CachingExecutor类的构造方法:

public CachingExecutor(Executor delegate) {    this.delegate = delegate;    delegate.setExecutorWrapper(this);}

6.拦截器链处理

pluginAll方法实现:

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

7.创建SqlSession实例

DefaultSqlSession类的构造:

public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {    this.configuration = configuration;    this.executor = executor;    this.autoCommit = autoCommit;}

二、源码分析

1.openSession()

SqlSession session = sqlSessionFactory.openSession();

2.openSessionFromDataSource()

根据默认ExecutorType创建事务上下文:

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

3.事务管理器创建

获取环境信息,创建事务工厂并初始化默认事务:

final Environment environment = configuration.getEnvironment();final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);

4.执行器创建

根据ExecutorType创建相应的执行器实例:

final Executor executor = configuration.newExecutor(tx, execType);

5.二级缓存配置

对二级缓存的具体操作:

public CachingExecutor(Executor delegate) {    this.delegate = delegate;    delegate.setExecutorWrapper(this);}

6.拦截器链处理

pluginAll方法实现:

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

7.创建SqlSession实例

DefaultSqlSession类的构造:

public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {    this.configuration = configuration;    this.executor = executor;    this.autoCommit = autoCommit;}

通过以上步骤,可以清晰地了解SqlSession对象的获取与创建过程,包括事务管理、执行器创建以及二级缓存配置等关键环节。

转载地址:http://mpwc.baihongyu.com/

你可能感兴趣的文章
OpenCV学堂 | YOLOv8实战 | 荧光显微镜细胞图像检测
查看>>
OpenCV官方文档 理解k - means聚类
查看>>
OpenCV探索
查看>>
OpenCV环境搭建(一)
查看>>
openCV目标识别 目标跟踪 YOLO5深度学习 Python 计算机视觉 计算机毕业设计 源码下载
查看>>
opencv笔记(1):图像缩放
查看>>
opencv笔记(二十四)——得到轮廓之后找到凸包convex hull
查看>>
OpenCV计算点到直线的距离 数学法
查看>>
Opencv识别图中人脸
查看>>
OpenCV读写avi、mpeg文件
查看>>
opencv面向对象设计初探
查看>>
OpenCV(1)读写图像
查看>>
OpenCV:不规则形状区域中每种颜色的像素数?
查看>>
OpenCV:概念、历史、应用场景示例、核心模块、安装配置
查看>>
OpenDaylight融合OpenStack架构分析
查看>>
OpenERP ORM 对象方法列表
查看>>
openEuler Summit 2022 成功举行,开启全场景创新新时代
查看>>
openEuler 正式开放:推动计算多样化时代的到来
查看>>
OpenEuler23.03欧拉系统_安装瀚高数据库企业版6.0.4_openeuler切换root用户_su:拒绝权限_passwd: 鉴定令牌操作错误---国产瀚高数据库工作笔记001
查看>>
OpenEuler23.03欧拉系统_安装瀚高数据库企业版6.0.4_踩坑_安装以后系统无法联网_启动ens33网卡---国产瀚高数据库工作笔记002
查看>>