mybatis源码分析之事务管理器
2019独角兽企业重金招聘Python工程师标准>>>
上一篇:mybatis源码分析之Configuration
主要分析了构建SqlSessionFactory的过程中配置文件的读取.
这次重点分析mybatis的事务管理器
仍旧从官网给的例子着手分析,配置文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><mapper resource="org/mybatis/example/BlogMapper.xml"/></mappers></configuration>
在根结点configuration下有environments和mappers两个结点.
environments:环境,mybatis可以配置多个环境即可以将 SQL 映射应用于多种数据库之中.如开发,测试,生产对应不同的数据库.
mappers:映射器,提供映射文件的地址.
在environment下的transactionManager就是现在要重点分析的对象.
mybatis中有两种类型的事务管理器,JDBC和MANAGED.
JDBC:直接使用了 JDBC 的提交和回滚设置,它依赖于从数据源得到的连接来管理事务范围。
MANAGED:不提交或回滚一个连接,而是让容器来管理事务的整个生命周期。
对例子进行分析,之前分析过
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
现在再来分析
SqlSession session = sqlSessionFactory.openSession();
DefaultSqlSessionFactory里的openSession
public SqlSession openSession() {return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
openSessionFromDataSource
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();}
}
看一下各个类之间的关系
JdbcTransaction和ManagedTransaction对应事务管理器的两种类型JDBC和MANAGED.
由于ManagedTransaction几乎没做什么,所以重点分析JdbcTransaction.
因为JdbcTransaction直接使用了 JDBC 的提交和回滚设置,它依赖于从数据源得到的连接来管理事务范围。
那么直接来看看JDBC的事务
java.sql.Connection
/*** A constant indicating that transactions are not supported.*/
int TRANSACTION_NONE = 0;/*** A constant indicating that* dirty reads, non-repeatable reads and phantom reads can occur.* This level allows a row changed by one transaction to be read* by another transaction before any changes in that row have been* committed (a "dirty read"). If any of the changes are rolled back,* the second transaction will have retrieved an invalid row.*/
int TRANSACTION_READ_UNCOMMITTED = 1;/*** A constant indicating that* dirty reads are prevented; non-repeatable reads and phantom* reads can occur. This level only prohibits a transaction* from reading a row with uncommitted changes in it.*/
int TRANSACTION_READ_COMMITTED = 2;/*** A constant indicating that* dirty reads and non-repeatable reads are prevented; phantom* reads can occur. This level prohibits a transaction from* reading a row with uncommitted changes in it, and it also* prohibits the situation where one transaction reads a row,* a second transaction alters the row, and the first transaction* rereads the row, getting different values the second time* (a "non-repeatable read").*/
int TRANSACTION_REPEATABLE_READ = 4;/*** A constant indicating that* dirty reads, non-repeatable reads and phantom reads are prevented.* This level includes the prohibitions in* <code>TRANSACTION_REPEATABLE_READ</code> and further prohibits the* situation where one transaction reads all rows that satisfy* a <code>WHERE</code> condition, a second transaction inserts a row that* satisfies that <code>WHERE</code> condition, and the first transaction* rereads for the same condition, retrieving the additional* "phantom" row in the second read.*/
int TRANSACTION_SERIALIZABLE = 8;
事务隔离级别
TRANSACTION_NONE:不支持事务
TRANSACTION_READ_UNCOMMITTED:允许脏读、不可重复的读和虚读.
TRANSACTION_READ_COMMITTED:不允许脏读,允许不可重复的读和虚读.
TRANSACTION_REPEATABLE_READ:不允许脏读和不可重复的读,允许虚读.
TRANSACTION_SERIALIZABLE:不允许脏读、不可重复的读和虚读.
脏读:一个事务对数据进行更新,但事务还没有提交,另一个事务就可以取到该事务没有提交的更新结果。
不可重复读:同一个事务在整个事务过程中对同一笔数据进行多次读取,每次读取结果都不同。
虚读:同一个查询在整个事务过程中多次执行后,查询所得的结果集是不一样的。虚读针对的是多条记录。
不可重复读:列值的不同; 虚读:记录数量的不同。
转载于:https://my.oschina.net/u/657390/blog/663080
mybatis源码分析之事务管理器相关推荐
- mybatis源码分析(2)——事务概述
这篇文章主要对mybatis中的事务做一简单的分析,帮助读者理清一些概念. 先来看看在JAVA事务的相关技术,在JAVA中有两类事务,JDBC事务和JTA事务,如果是JDBC类型的事务,则是由Conn ...
- MyBatis 源码分析 - 缓存原理
1.简介 在 Web 应用中,缓存是必不可少的组件.通常我们都会用 Redis 或 memcached 等缓存中间件,拦截大量奔向数据库的请求,减轻数据库压力.作为一个重要的组件,MyBatis 自然 ...
- 源码通透-mybatis源码分析以及整合spring过程
源码通透-mybatis源码分析以及整合spring过程 mybatis源码分析版本:mybaits3 (3.5.0-SNAPSHOT) mybatis源码下载地址:https://github.co ...
- MyBatis源码分析(一)MyBatis整体架构分析
文章目录 系列文章索引 一.为什么要用MyBatis 1.原始JDBC的痛点 2.Hibernate 和 JPA 3.MyBatis的特点 4.MyBatis整体架构 5.MyBatis主要组件及其相 ...
- MyBatis 源码分析 - 配置文件解析过程
文章目录 * 本文速览 1.简介 2.配置文件解析过程分析 2.1 配置文件解析入口 2.2 解析 properties 配置 2.3 解析 settings 配置 2.3.1 settings 节点 ...
- MyBatis 源码分析 - 内置数据源
1.简介 本篇文章将向大家介绍 MyBatis 内置数据源的实现逻辑.搞懂这些数据源的实现,可使大家对数据源有更深入的认识.同时在配置这些数据源时,也会更清楚每种属性的意义和用途.因此,如果大家想知其 ...
- MyBatis 源码分析 - SQL 的执行过程
本文速览 本篇文章较为详细的介绍了 MyBatis 执行 SQL 的过程.该过程本身比较复杂,牵涉到的技术点比较多.包括但不限于 Mapper 接口代理类的生成.接口方法的解析.SQL 语句的解析.运 ...
- MyBatis 源码分析系列文章导读
1.本文速览 本篇文章是我为接下来的 MyBatis 源码分析系列文章写的一个导读文章.本篇文章从 MyBatis 是什么(what),为什么要使用(why),以及如何使用(how)等三个角度进行了说 ...
- MyBatis 源码分析系列文章导读 1
1.本文速览 本篇文章是我为接下来的 MyBatis 源码分析系列文章写的一个导读文章.本篇文章从 MyBatis 是什么(what),为什么要使用(why),以及如何使用(how)等三个角度进行了说 ...
最新文章
- 基于Python的QPSK音频的波形和频谱
- 制作TortoiseSVN最新版本的中文DLL(转)
- 十三种技术文档模板_在线文档,知多少?
- 浮点类型误区 “!=”
- oracle查zw001密码,【Oracle错误集锦】:ORA-00119amp;ORA-00132-一团网
- python 调用外部程序 终端异常_python调用外部命令
- python selenium 对浏览器标签页进行关闭和切换
- Mac无损音乐播放器:Audirvana
- javascript 3月17日
- mysql query cache_MySQL Query Cache开启与否的必要性分析
- PySide2将控制台内容打印到textEdit控件
- 怎么查看虚拟机的IP地址?
- 人类微生物组和缺失遗传力--读论文
- OEM JDM ODM OBM的区别
- 2020年CSP-J2 CSP-S2 复赛题解
- Tushare股票数据全生命周期MACD等指标动态获取计算-前复权
- 什么是固态硬盘?它有什么好处?
- J-Link软件和文档包的版本发行说明(2)[V4.96 ~ V6.12j版本]
- 数电实验_时分秒计数器——终极版
- Maven连接MySQL数据库
热门文章
- matlab 线模式密度,环形腔窄线宽光纤激光器的研究
- linux挂载移动硬盘 格式化_linux系统下如何挂载NTFS移动硬盘
- Mybatis复习笔记3:映射文件详解
- php 进程管理,php如何管理进程
- matlab 迭代 混沌与分形实验报告,实验四 函数的迭代混沌与分形.doc
- 【jquery】jquery选择器
- javascript-对混合字母/数字数组进行排序
- 使用Docsify搭建Markdown文件服务器
- Lighttpd1.4.20源代码分析 笔记 状态机之错误处理和连接关闭
- mongodb3 分片集群平滑迁移