参考博客:https://cloud.tencent.com/developer/article/1529692

看sharding-jdbc支持XA协议重点看下面的代码

sharding-transaction-xa-atomikos模块中主要是原生的atomikos的配置,atomikos事务配置都是在transactions.properties中进行配置的

sharding-transaction模块由sharding-transaction-core,sharding-transaction-2pc和sharding-transaction-base这3个子模块组成。

Apache ShardingSphere(Incubating)能够自动将XADataSource作为数据库驱动的数据源接入XA事务管理器。而针对于使用DataSource作为数据库驱动的应用,用户也无需改变其编码以及配置,Apache ShardingSphere(Incubating)通过自动适配的方式,在中间件内部将其转化为支持XA协议的XADataSource和XAConnection,并将其作为XA资源注册到底层的XA事务管理器中。

ShardingSphere还会实现XA协议的recovery部分,即在事务处理器出现崩溃的情况时,可以有能力提供in-doubt transactions来实现事务恢复。

不支持informix数据库atomikosTransactionManager

sharding-jdbc支持事务上面的两个注解是需要的

@Transactional表示底层使用spring的事务管理,spring底层的事务管理器使用默认的com.atomikos.icatch.jta.UserTransactionManager

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--==========针对两个库,各配置一个AtomikosDataSourceBean,底层都使用MysqlXADataSource=====================-->
<!--配置数据源db_user-->
<bean id="db_user" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="ds1" />
<property name="xaDataSourceClassName"
value="com.mysql.cj.jdbc.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="url">jdbc:mysql://localhost:3306/db_user?serverTimezone=UTC</prop>
<prop key="user">root</prop>
<prop key="password"></prop>
</props>
</property>
</bean> <!--配置数据源db_account-->
<bean id="db_account" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="ds2" />
<property name="xaDataSourceClassName"
value="com.mysql.cj.jdbc.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="url">jdbc:mysql://localhost:3306/db_account?serverTimezone=UTC</prop>
<prop key="user">root</prop>
<prop key="password"></prop>
</props>
</property>
</bean> <!--=============针对两个数据源,各配置一个SqlSessionFactoryBean============ -->
<bean id="ssf_user" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="db_user" />
</bean> <bean id="ssf_account" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="db_account" />
</bean> <!--=============针对两个SqlSessionFactoryBean,各配置一个MapperScannerConfigurer============ -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="ssf_user"/>
<!--指定com.tianshouzhi.atomikos.mappers.db_user包下的UserMapper接口使用ssf_user获取底层数据库连接-->
<property name="basePackage" value="com.tianshouzhi.atomikos.mappers.db_user"/>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="ssf_account"/>
<!--指定com.tianshouzhi.atomikos.mappers.ds_account包下的AccountMapper接口使用ssf_account获取底层数据库连接-->
<property name="basePackage" value="com.tianshouzhi.atomikos.mappers.ds_account"/>
</bean> <!--================配置atomikos事务管理器========================-->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init"
destroy-method="close">
<property name="forceShutdown" value="false"/>
</bean> <!--============配置spring的JtaTransactionManager,底层委派给atomikos进行处理===============-->
<bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager"/>
</bean> <!--配置spring声明式事务管理器-->
<tx:annotation-driven transaction-manager="jtaTransactionManager"/> <bean id="jtaService" class="com.tianshouzhi.atomikos.JTAService"/>
</beans>

第二个注解 @ShardingTransactionType(TransactionType.XA)也是需要的,改注解sharding-jdbc将一般的数据源拦截成XADatasource,将一般的事务管理器封装成XA的事务管理器atomikosTransactionManager

这里的原理在于

https://blog.csdn.net/liu1390910/article/details/94554356

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# com.atomikos.icatch.serial_jta_transactions = false
com.atomikos.icatch.default_jta_timeout =
com.atomikos.icatch.max_actives =
com.atomikos.icatch.checkpoint_interval =
com.atomikos.icatch.enable_logging = true
com.atomikos.icatch.log_base_name = xa_tx
com.atomikos.icatch.log_base_dir = ./logs

atomikos 创建数据源,报Max number of active transactions

 

技术小美 2017-11-15 13:25:00 浏览1828

 
在使用atomikos 事务管理中,当并发数超过50的时候会产生异常如下:
java.lang.IllegalStateException:Max number of active transactions reched:50

原因:

atomikos的默认配置中 transactions.properties中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# SAMPLE PROPERTIES FILE FOR THE TRANSACTION SERVICE
# THIS FILE ILLUSTRATES THE DIFFERENT SETTINGS FOR THE TRANSACTION MANAGER
# UNCOMMENT THE ASSIGNMENTS TO OVERRIDE DEFAULT VALUES;
 
# Required: factory implementation class of the transaction core.
# NOTE: there is no default for this, so it MUST be specified! 
com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
 
         
# Set base name of file where messages are output 
# (also known as the 'console file').
#
# com.atomikos.icatch.console_file_name = tm.out
 
# Size limit (in bytes) for the console file;
# negative means unlimited.
#
# com.atomikos.icatch.console_file_limit=-1
 
# For size-limited console files, this option
# specifies a number of rotating files to 
# maintain.
#
# com.atomikos.icatch.console_file_count=1
 
# Set the number of log writes between checkpoints
#
# com.atomikos.icatch.checkpoint_interval=500
 
# Set output directory where console file and other files are to be put
# make sure this directory exists!
#
# com.atomikos.icatch.output_dir = ./
 
# Set directory of log files; make sure this directory exists!
#
# com.atomikos.icatch.log_base_dir = ./
 
# Set base name of log file
this name will be  used as the first part of 
# the system-generated log file name
#
# com.atomikos.icatch.log_base_name = tmlog
 
# Set the max number of active local transactions 
# or -1 for unlimited.
#
# com.atomikos.icatch.max_actives = 50 (原因)
 
# Set the default timeout (in milliseconds) for local transactions
#
# com.atomikos.icatch.default_jta_timeout = 10000
 
# Set the max timeout (in milliseconds) for local transactions
#
# com.atomikos.icatch.max_timeout = 300000
 
# The globally unique name of this transaction manager process
# override this value with a globally unique name
#
# com.atomikos.icatch.tm_unique_name = tm
     
# Do we want to use parallel subtransactions? JTA's default
# is NO for J2EE compatibility
#
# com.atomikos.icatch.serial_jta_transactions=true
                     
# If you want to do explicit resource registration then
# you need to set this value to false.
#
# com.atomikos.icatch.automatic_resource_registration=true  
     
# Set this to WARN, INFO or DEBUG to control the granularity
# of output to the console file.
#
# com.atomikos.icatch.console_log_level=WARN
     
# Do you want transaction logging to be enabled or not?
# If set to false, then no logging overhead will be done
# at the risk of losing data after restart or crash.
#
# com.atomikos.icatch.enable_logging=true
 
# Should two-phase commit be done in (multi-)threaded mode or not?
# Set this to false if you want commits to be ordered according
# to the order in which resources are added to the transaction.
#
# NOTE: threads are reused on JDK 1.5 or higher. 
# For JDK 1.4, thread reuse is enabled as soon as the 
# concurrent backport is in the classpath - see 
# http://mirrors.ibiblio.org/pub/mirrors/maven2/backport-util-concurrent/backport-util-concurrent/
#
# com.atomikos.icatch.threaded_2pc=false
 
# Should shutdown of the VM trigger shutdown of the transaction core too?
#
# com.atomikos.icatch.force_shutdown_on_vm_exit=false

修改默认配置中的:

1
com.atomikos.icatch.max_actives = 50------改为更大就可以解决
 

使用atomikos时,事务默认超时时间是100000毫秒,超过这个时间,提交事务就会抛出异常com.atomikos.icatch.RollbackException: Prepare: NO vote。

今天总算通过bing找到了答案,记录一下。

在网上很多说的要设置com.atomikos.icatch.max_timeout和com.atomikos.icatch.default_jta_timeout,居然都没说怎么设置,集成Spring的情况下,第一时间想到在bean的属性里配置,结果没找到(default_jta_timeout可以在org.springframework.transaction.jta.JtaTransactionManager中找到defaultTimeOut属性与之匹配,但是max_timeout没有找到,官方文档(https://www.atomikos.com/Documentation/JtaProperties)也没说,只是说设置max_timeout和UserTransaction.setTransactionTimeout()是一个意思,结果我配置了半天,没有效果)。最后搞明白了,需要在classpath下建一个jta.properties(或者transactions.properties)文件(事务管理器的配置),来配置事务相关属性,如下是我的jta.properties。

PS:default_jta_timeout表示开启事务时,默认的超时时间,max_timeout表示最大的超时时间,0表示无限时间。如果default_jta_timeout设置的值超过了max_timeout,会自动将超时时间截断,使用max_timeout的值(日志会打印出来)。

然后,Spring的配置文件

<!-- Atomikos 事务管理器配置 -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<!-- <property name="startupTransactionService" value="false" /> -->
<!-- close()时是否强制终止事务 -->
<property name="forceShutdown" value="false" />
</bean> <!-- Atomikos UserTransaction配置 -->
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"></bean> <!-- JTA事务管理器 -->
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<ref bean="atomikosTransactionManager" />
</property>
<property name="userTransaction">
<ref bean="atomikosUserTransaction" />
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" />

为XA和非XA提供内置的JDBC适配器,所有不再需要配置多余的东西

sharding-transaction-core主要的功能在于ShardingTransactionManager类以spi扩展的是是哪些事务类型来生成对于的事务管理器,例如当前是XA的atomitx事务管理器,那么这里ShardingTransactionManager对于的事务管理器就是com.atomikos.icatch.jta.UserTransactionManager

sharding-transaction-spring模块就是扫描断言的,当你的方法配置了@ShardingTransactionType (TransactionType.XA)注解之后,harding-transaction-spring模块会拦截到改注解,通过Threadlocal切换当前事务类型,适用于Sharding-JDBC。例如:TransactionTypeHolder.set (TransactionType.XA),代码如下

Apache ShardingSphere(Incubating)官方目前实现了基于Atomikos和Bitronix的SPI,并且邀请了Radhat JBoss的XA事务引擎Narayana [https://github.com/jbosstm/narayana] 开发团队实现了JBoss的SPI。用户可以自行的在Atomikos,Bitronix和Narayana间选择自己喜欢的XA事务管理器。

sharding-transaction-xa-sp模块就是sharding-jdbc提供的模块,将外部的XAresource资源封装到事务管理器中XATransactionManager中,这里XA

sharding-jdbc源码解析的更多相关文章

  1. Solr DIH JDBC 源码解析

    Solr DIH 源码解析 DataImportHandler.handleRequestBody()中的importer.runCmd(requestParams, sw) if (DataImpo ...

  2. JDBC源码解析

    参考:https://blog.csdn.net/silviakafka/article/details/46225045

  3. MySQL的JDBC驱动源码解析

    原文:   MySQL的JDBC驱动源码解析 大家都知道JDBC是Java访问数据库的一套规范,具体访问数据库的细节有各个数据库厂商自己实现 Java数据库连接(JDBC)由一组用 Java 编程语言 ...

  4. Spring框架之jdbc源码完全解析

    Spring框架之jdbc源码完全解析 Spring JDBC抽象框架所带来的价值将在以下几个方面得以体现: 1.指定数据库连接参数 2.打开数据库连接 3.声明SQL语句 4.预编译并执行SQL语句 ...

  5. Mybatis源码解析1—— JDBC

    在之前的文章中,我为大家介绍了 Mybatis 的详细用法,算是基础教程. 详细链接:Mybatis 基础教程 言归正传,只懂基础可不行,接下来将给大家带来高阶的源码解析教程,从浅入深,通过源码解析, ...

  6. spring事务源码解析

    前言 在spring jdbcTemplate 事务,各种诡异,包你醍醐灌顶!最后遗留了一个问题:spring是怎么样保证事务一致性的? 当然,spring事务内容挺多的,如果都要讲的话要花很长时间, ...

  7. 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析

    前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...

  8. MyBatis源码解析(四)——DataSource数据源模块

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6634880.html 1.回顾 上一文中解读了MyBatis中的事务模块,其实事务操作无非 ...

  9. mybatis源码解析1--前言

    在开始分析mybatis源码之前,需要定一个目标,也就是我们不是为了读源码而去读,一定是带着问题去读,在读的时候去寻找到答案,然后再读码的同时整理总结,学习一些高级的编码方式和技巧. 首先我们知道my ...

  10. Mybatis 系列8-结合源码解析select、resultMap的用法

    [Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...

随机推荐

  1. git status 命令详解

    git status命令表示:文件,文件夹在工作区,暂存区的状态,下图就是文件,文件夹三种状态: Changes to be committed: (use "git restore --s ...

  2. 拨号vps,拨号vps是什么意思干什么用的,如何使用拨号vps

    首先,拨号vps是动态IP的VPS.vps虚拟服务器.拨号服务器.有些业务,如刷单.投票等操作对ip地址有限制,不能过多的使用.而拨号VPS通过拨号上网,每拨号一次号,就变一次IP,完成ip地址的动态 ...

  3. 1. vue文件结构

    1)这是文件规则 <template> <div > </div> </template> <script> Name:"当前的名 ...

  4. Chisel3 - Wire & Reg

    https://mp.weixin.qq.com/s/Y26N5P4XOr5e3uyi5XQY-w   不同于Verilog,Chisel中Wire和Reg并不是数据类型,而是数据容器,作为数据的一个 ...

  5. Java并发编程 (七) J.U.C之AQS

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一. J.U.C之AQS-介绍 1.定义: AbstractQueuedSynchronizer简称AQ ...

  6. css3 属性阴影效果--box-shadow,text-shadow

    1.text-shadow:h-shadow v-shadow blur color; h-shadow:水平阴影的位置,可以是负值,正值向右,负值向左 v-shadow:水平阴影的位置,可以是负值, ...

  7. Java实现 LeetCode 485 最大连续1的个数

    485. 最大连续1的个数 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数 ...

  8. Java实现 LeetCode 409 最长回文串

    409. 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意 ...

  9. Java实现 洛谷 P2024 [NOI2001]食物链

    输入输出样例 输入 #1 100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5 输出 #1 3 import java.util.Scanner; pub ...

  10. java实现第六届蓝桥杯饮料换购

    饮料换购 饮料换购 乐羊羊饮料厂正在举办一次促销优惠活动.乐羊羊C型饮料,凭3个瓶盖可以再换一瓶C型饮料,并且可以一直循环下去,但不允许赊账. 请你计算一下,如果小明不浪费瓶盖,尽量地参加活动,那么, ...