参考博客: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">123456</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">123456</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 = 300000 com.atomikos.icatch.max_actives = 10000 com.atomikos.icatch.checkpoint_interval = 50000 com.atomikos.icatch.enable_logging = true com.atomikos.icatch.log_base_name = xa_tx com.atomikos.icatch.log_base_dir = ./logs