3.ibatis4种事务类型浅析

Wesley13
• 阅读 506

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 name="dataSource" ref="dataSource">
<property name="transactionConfigClass" value="com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransactionConfig">

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.javasqlMapClientTemplate获取连接时

 3.ibatis4种事务类型浅析

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这