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种事务类型浅析的更多相关文章

  1. java的事务类型及定义

    转载: 什么是事务: 首先,说说什么事务.我认为事务,就是一组操作数据库的动作集合. 事务是现代数据库理论中的核心概念之一.如果一组处理步骤或者全部发生或者一步也不执行,我们称该组处理步骤为一个事务. ...

  2. Spring7种事务传播行为类型--PROPAGATION_REQUIRED及其他6种事务传播行为种类

    PROPAGATION_REQUIRED及其他6种事务传播行为种类,有需要的朋友可以参考下. Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务 ...

  3. Spring 7种事务传播类型

    转载:https://www.cnblogs.com/originate918/p/6226342.html PROPAGATION_REQUIRED及其他6种事务传播行为种类. Spring在Tra ...

  4. TiKV事务实现浅析

    TiKV事务实现浅析 Percolator事务的理论基础 Percolator的来源 Percolator事务来源于Google在设计更新网页索引的系统时提出的论文Large-scale Increm ...

  5. Mysql表的七种引擎类型,InnoDB和MyISAM引擎对比区别总结

    InnoDB和MyISAM区别总结 我用MySQL的时候用的是Navicat for MySQL(Navicat for mysql v9.0.15注册码生成器)操作库.表操作的,默认的表就是Inno ...

  6. MySQL常用的七种表类型(转)

    MySQL常用的七种表类型(转)   其实MySQL提供的表类型截至到今天已经有13种,各有各的好处,但是民间流传的常用的应该是7种,如果再细化出来,基本上就只有两种:InnoDB.MyIASM两种. ...

  7. 按照事务类型分析 DB2 事物的性能

    概述 事务是数据库系统中的核心概念之一.作为数据库系统的逻辑工作单元(Unit of Work),事务必须具有四个属性,即原子性.一致性.隔离性和持久性(ACID).数据库系统往往通过锁机制保证事务的 ...

  8. FMDB支持的事务类型

    FMDB支持的事务类型   在数据库中,事务可以保证数据操作的完整性.当存在大量并发操作,容易出现死锁问题.在SQLite中,为了解决该问题,提供三种事务模式,分别为DEFFERED.IMMEDIAT ...

  9. 手把手带你实战下Spring的七种事务传播行为

    目录 本文目录 一.什么是事务传播行为? 二.事务的7种传播行为 三.7种传播行为实战 本文介绍Spring的七种事务传播行为并通过代码演示下. 本文目录 一.什么是事务传播行为? 事务传播行为(pr ...

随机推荐

  1. vue报类似警告Computed property "isLoading" was assigned to but it has no setter

    一.原因:一个计算属性,当计算传入的是一个函数,或者传入的是一个对象,而没有设置 setter,也就是 set 属性,当你尝试直接该改变这个这个计算属性的值,都会报这个警告,vuex还会出现通过com ...

  2. js 中, set 与 数组 相互转换

    主要用 Array.from 方法 1.array --> set (数组转set) let array = [1, 2, 3, 4]; let set = new Set(array); 2. ...

  3. WeakReference 与 SoftReference 区别

    装载自:http://flyneil.iteye.com/blog/1345177 WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关. Wea ...

  4. 17.SpringMVC核心技术-拦截器

    SpringMVC 中的 Interceptor 拦截器是非常重要和相当有用的,它的主要作用是拦截指定 的用户请求, 并进行相应的预处理与后处理.其拦截的时间点在“处理器映射器根据用户提 交的请求映射 ...

  5. 解决maven 引用JDK内部类编译错误 程序包:com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler不存在

    当maven项目里面有用到JDK内部的一些类或者接口的时候,用maven编译一般会出现如下错误: 程序包:com.sun.xml.internal.bind.marshaller.CharacterE ...

  6. RabbitMQ的持久化

      RabbitMQ的持久化主要体现在三个方面,即交换机持久化,队列持久化及消息持久化 注意,因公司使用php-amqplib来实现RabbitMQ,故之后举例说明的代码均使用的php-amqplib ...

  7. PHP点击按钮拷贝

    一.PHP中点击按钮拷贝文本,我一个页面有多个按钮,相同颜色的标注代表了相同的列或ID 二.这是HTML代码 <tr> <td>直播流地址(延时60秒)</td> ...

  8. 谷歌浏览器安装xpath使用

    一.Xpath-helper插件说明 谷歌浏览的插件,目的是可以定位到具体的元素中,实时验证xpath是不是正确 谷歌插件下载位置:https://chrome.google.com/webstore ...

  9. tensorflow 更新出现HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

    pip install --upgrade tensorflow --default-timeout=1000

  10. CSS测试题Ⅱ

    1.如何使用 CSS3 强制换行? A. word-wrap: break-word; B. text-wrap: break-word; C. text-wrap: force; D. text-w ...