3.ibatis4种事务类型浅析
ibatis中Transaction有四个实现类
其中spring的SqlMapClientFactoryBean类中
private Class transactionConfigClass = ExternalTransactionConfig.class;
public SqlMapClientFactoryBean() {
this.transactionConfigProperties = new Properties();
this.transactionConfigProperties.setProperty("SetAutoCommitAllowed", "false");
}
1.ExternalTransaction
- 不指定事务类型时,默认使用此类型的事务;
- 在调用getConnection()时才去建立数据库连接;
- AutoCommit默认为true;
- commit()和rollback()均为空实现;
public ExternalTransaction(DataSource ds, boolean defaultAutoCommit, boolean setAutoCommitAllowed, int isolationLevel) throws TransactionException {
this.dataSource = ds;
if (this.dataSource == null) {
throw new TransactionException("ExternalTransaction initialization failed. DataSource was null.");
} else {
this.defaultAutoCommit = defaultAutoCommit;
this.setAutoCommitAllowed = setAutoCommitAllowed;
this.isolationLevel.setIsolationLevel(isolationLevel);
}
}
public Connection getConnection() throws SQLException, TransactionException {
if (this.connection == null) {
this.init();
}
return this.connection;
}
private void init() throws SQLException, TransactionException {
this.connection = this.dataSource.getConnection();
if (this.connection == null) {
throw new TransactionException("ExternalTransaction could not start transaction. Cause: The DataSource returned a null connection.");
} else {
this.isolationLevel.applyIsolationLevel(this.connection);
if (this.setAutoCommitAllowed && this.connection.getAutoCommit() != this.defaultAutoCommit) {
this.connection.setAutoCommit(this.defaultAutoCommit);
}
if (connectionLog.isDebugEnabled()) {
this.connection = ConnectionLogProxy.newInstance(this.connection);
}
}
}
2.JdbcTransaction
- 需要在配置文件中指定transactionConfigClass为此类型;
- 在调用getConnection()时才去建立数据库连接,同上;
- AutoCommit默认为false;
- 由TransactionManager统一管理,在执行过程中自动调用commit()和rollback()提交或者回滚;
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sql-map-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="transactionConfigClass" value="com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransactionConfig"></property>
</bean>
public void commit() throws SQLException, TransactionException {
if (this.connection != null) {
this.connection.commit();
}
}
public void rollback() throws SQLException, TransactionException {
if (this.connection != null) {
this.connection.rollback();
}
}
3.JtaTransaction
Todo:暂时还未看到,待补充。。。。。。。。。。。。。。。
4.UserProviderTransaction
- 不能在配置文件中指定此类型的事务,需要先有Connection的情况下才可以使用此类型,即:在TransactionManager的begin()方法中不会new出此类型的事务;
- AutoCommit为之前获取的Connection设置的值,默认为true;
- commit()和rollback()没有发现哪里使用;
- 在使用spring的SqlMapClientTemplate操作时,会根据获取到的SpringCon组装一个UserProviderTransaction,此时配置文件中设置的上述三种事务类型均失效;
个人理解:
如果外层不手动配置事务,此事务的作用仅仅是提供了一个connection而已,此时connection.getAutoCommit()==true,并没有真正起到事务的作用;
如果外层配置了事务,此事务跟外层配置的事务使用同一个connection,外层事务会设置connection的Autocommit=false,真正起作用的还是外层的事务。
public UserProvidedTransaction(Connection connection) {
if (connectionLog.isDebugEnabled()) {
this.connection = ConnectionLogProxy.newInstance(connection);
} else {
this.connection = connection;
}
}
public Connection getConnection() throws SQLException, TransactionException {
return this.connection;
}
参考流程:
当外部配置事务时,开启事务时会先获取一个connection
DataSourceTransactionManager.java![]()
sqlMapClientTemplate获取连接时

3.ibatis4种事务类型浅析的更多相关文章
- java的事务类型及定义
转载: 什么是事务: 首先,说说什么事务.我认为事务,就是一组操作数据库的动作集合. 事务是现代数据库理论中的核心概念之一.如果一组处理步骤或者全部发生或者一步也不执行,我们称该组处理步骤为一个事务. ...
- Spring7种事务传播行为类型--PROPAGATION_REQUIRED及其他6种事务传播行为种类
PROPAGATION_REQUIRED及其他6种事务传播行为种类,有需要的朋友可以参考下. Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务 ...
- Spring 7种事务传播类型
转载:https://www.cnblogs.com/originate918/p/6226342.html PROPAGATION_REQUIRED及其他6种事务传播行为种类. Spring在Tra ...
- TiKV事务实现浅析
TiKV事务实现浅析 Percolator事务的理论基础 Percolator的来源 Percolator事务来源于Google在设计更新网页索引的系统时提出的论文Large-scale Increm ...
- Mysql表的七种引擎类型,InnoDB和MyISAM引擎对比区别总结
InnoDB和MyISAM区别总结 我用MySQL的时候用的是Navicat for MySQL(Navicat for mysql v9.0.15注册码生成器)操作库.表操作的,默认的表就是Inno ...
- MySQL常用的七种表类型(转)
MySQL常用的七种表类型(转) 其实MySQL提供的表类型截至到今天已经有13种,各有各的好处,但是民间流传的常用的应该是7种,如果再细化出来,基本上就只有两种:InnoDB.MyIASM两种. ...
- 按照事务类型分析 DB2 事物的性能
概述 事务是数据库系统中的核心概念之一.作为数据库系统的逻辑工作单元(Unit of Work),事务必须具有四个属性,即原子性.一致性.隔离性和持久性(ACID).数据库系统往往通过锁机制保证事务的 ...
- FMDB支持的事务类型
FMDB支持的事务类型 在数据库中,事务可以保证数据操作的完整性.当存在大量并发操作,容易出现死锁问题.在SQLite中,为了解决该问题,提供三种事务模式,分别为DEFFERED.IMMEDIAT ...
- 手把手带你实战下Spring的七种事务传播行为
目录 本文目录 一.什么是事务传播行为? 二.事务的7种传播行为 三.7种传播行为实战 本文介绍Spring的七种事务传播行为并通过代码演示下. 本文目录 一.什么是事务传播行为? 事务传播行为(pr ...
随机推荐
- vue cli创建脚手架
1.用vscode打开一个文件夹.在菜单栏 点击 查看-集成终端.这里可以用其他的方法比如cmd命令符调开这个界面,但是要用cd 切到要放文件的文件夹下. 2.安装好node.js 和淘宝镜像 3. ...
- Linux的bg和fg命令
我们都知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行,要么停止这个服务.而不能让程序在前台后台之间切换.而 Linux 提供了 fg 和 bg 命令,让我们轻松调度正在运行的 ...
- Ubuntu系统--安装官方flash插件包的方法
浏览器安装官方install_flash_player_npapi_linux.x86_64.tar.gz插件包的方法 第一步:下载安装包. adobe flash player的官方下载instal ...
- 2019 Petrozavodsk Winter Camp, Yandex Cup C. Diverse Singing 上下界网络流
建图一共建四层 第一层为N个歌手 第二层为{pi,li} 第三层为{si,li} 第四层为M首歌 除了S和第一层与第三层与T之间的边为[1,INF] 其他边均为[0,1] #include<bi ...
- C#中怎么将XML作为参数post到接口
String xml = "<data>中文</data>"; String postData = "data=" + Server.U ...
- string::copy
size_t copy (char* s, size_t len, size_t pos = 0) const;功能:把string的pos位置开始的len字节copy到s注意:s的最后要手动添加字符 ...
- python_函数作用域
py文件:全局作用域 函数:局部作用域 一个函数是一个作用域 def func(): x = 9 print(x) func() print(x) 作用域中查找数据规则:优先在自己的作用域找数据,自己 ...
- CSS3 -- 边框圆角
文章后有彩蛋哦 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- metal feature and specification
https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf 宝贝 https://developer.apple.com/metal/ ...
- Java实现文件的上传下载(含源代码和jar包)
1.需要使用的jar包 链接:https://pan.baidu.com/s/1IaxQRSwfzxDpe4w4JiaEKw 提取码:xwtz 2.如果想实现文件的下载,需要创建一张表,表的结构为 i ...

sqlMapClientTemplate获取连接时