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. Spring Cloud(六)服务网关 zuul 快速入门

    服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能.Spring Cloud Netflix中 ...

  2. shell菜单选择

    我们会遇到很多进入后台系统的时候,会根据选择,进入不同的系统,下面是一个简单的例子: #!/bin/sh function menu (){ cat << EOF------------- ...

  3. 离线文档 real

    mac 上用 dash windows 用 real 上官网下载安装 tool-> 安装需要的文档 就可以使用. 没有难度,记录一下.

  4. Image Processing and Computer Vision_Review:Local Invariant Feature Detectors: A Survey——2007.11

    翻译 局部不变特征探测器:一项调查 摘要 -在本次调查中,我们概述了不变兴趣点探测器,它们如何随着时间的推移而发展,它们如何工作,以及它们各自的优点和缺点.我们首先定义理想局部特征检测器的属性.接下来 ...

  5. Window10下Python3.7的wordcloud库的安装与基本使用

    1.进入Python官网→点击Pypl→搜索“wordcloud”.如下图所示: 2.使用cmd安装,具体操作如下: 使用 pip list 查看是否安装成功

  6. 018.查询练习50题(sql实例)

    CREATE TABLE EMP(EMPNO numeric(5,0) NOT NULL primary key,--雇员的编号ENAME nvarchar(10) not null,--雇员的名字J ...

  7. Maven 安装依赖包

    Guide to installing 3rd party JARs Although rarely, but sometimes you will have 3rd party JARs that ...

  8. __builtin_ _Find_first()

    •int __builtin_ffs (unsigned int x) 返回x的最后一位1的是从后向前第几位,比如7368(1110011001000)返回4. •int __builtin_clz ...

  9. iOS 中通过kvc 获取数组的均值、求和、最大最小值等

    NSArray *values = @[@, @, @, @, @, @, @, @, @, @, @, @, @, @, @, @, @]; NSNumber *avg = [values valu ...

  10. CF827D Best Edge Weight[最小生成树+树剖/LCT/(可并堆/set启发式合并+倍增)]

    题意:一张图求每条边边权最多改成多少可以让所有MST都包含这条边. 这题还是要考察Kruskal的贪心过程. 先跑一棵MST出来.然后考虑每条边. 如果他是非树边,要让他Kruskal的时候被选入,必 ...