Spring跨数据库事务管理
1.POM文件配置
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>atomikos-util</artifactId>
<version>3.9.3</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions</artifactId>
<version>3.9.3</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jta</artifactId>
<version>3.9.3</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<version>3.9.3</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
2.Spring文件配置
<?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-3.2.xsd">
<bean id="pagePlugin" class="com.github.pagehelper.PageHelper">
<property name="properties">
<props>
<prop key="dialect">mysql</prop>
<prop key="pageSizeZero">true</prop>
</props>
</property>
</bean>
<bean id="abstractXADataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close" abstract="true">
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
<property name="poolSize" value="10" />
<property name="minPoolSize" value="10"/>
<property name="maxPoolSize" value="30"/>
<property name="borrowConnectionTimeout" value="60"/>
<property name="reapTimeout" value="20"/>
<property name="maxIdleTime" value="60"/>
<property name="maintenanceInterval" value="60" />
<property name="loginTimeout" value="60"/>
<property name="logWriter" value="60"/>
<property name="testQuery">
<value>select 1</value>
</property>
</bean>
<bean id="datasource_one" parent="abstractXADataSource">
<property name="uniqueResourceName" value="mysql/sitestone" />
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="url">${jdbc.one.url}</prop>
<prop key="user">${jdbc.one.user}</prop>
<prop key="password">${jdbc.one.password}</prop>
<prop key="pinGlobalTxToPhysicalConnection">true</prop>
</props>
</property>
</bean>
<bean id="datasource_two" parent="abstractXADataSource">
<property name="uniqueResourceName" value="mysql/sitestwo" />
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="url">${jdbc.two.url}</prop>
<prop key="user">${jdbc.two.user}</prop>
<prop key="password">${jdbc.two.password}</prop>
<prop key="pinGlobalTxToPhysicalConnection">true</prop>
</props>
</property>
</bean>
<bean id="datasource_three" parent="abstractXADataSource">
<property name="uniqueResourceName" value="mysql/sitestthree" />
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="url">${jdbc.three.url}</prop>
<prop key="user">${jdbc.three.user}</prop>
<prop key="password">${jdbc.three.password}</prop>
<prop key="pinGlobalTxToPhysicalConnection">true</prop>
</props>
</property>
</bean>
<bean id="dynamicDataSource" class="com.company.common.CustomerDatasource">
<property name="defaultTargetDataSource" ref="datasource_one"/>
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="warehouse" value-ref="datasource_one"/>
<entry key="purchase" value-ref="datasource_two"/>
<entry key="stock" value-ref="datasource_three"/>
</map>
</property>
</bean>
<bean id="sqlSessionFactorya" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource_one"/>
<property name="typeAliasesPackage" value="com.company.one.entity" />
<property name="mapperLocations" value="classpath:mybatis/*Mapper.xml" />
<property name="plugins">
<array>
<ref bean="pagePlugin" />
</array>
</property>
</bean>
<bean id="sqlSessionFactoryb" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource_two"/>
<property name="typeAliasesPackage" value="com.company.two.entity" />
<property name="mapperLocations" value="classpath:mybatis/*Mapper.xml" />
<property name="plugins">
<array>
<ref bean="pagePlugin" />
</array>
</property>
</bean>
<bean id="sqlSessionFactoryc" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource_three"/>
<property name="typeAliasesPackage" value="com.company.three.entity"/>
<property name="mapperLocations" value="classpath:mybatis/*Mapper.xml"/>
<property name="plugins">
<array>
<ref bean="pagePlugin"/>
</array>
</property>
</bean>
<bean id="sqlSessionTemplate" class="com.yyt.newshop.erp.warehouse.common.CustomSqlSessionTemplate" scope="prototype">
<constructor-arg ref="sqlSessionFactorya" />
<property name="targetSqlSessionFactorys">
<map>
<entry value-ref="sqlSessionFactorya" key="one"/>
<entry value-ref="sqlSessionFactoryb" key="two"/>
<entry value-ref="sqlSessionFactoryc" key="three" />
</map>
</property>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
<property name="basePackage" value="com.company.dao" />
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown">
<value>true</value>
</property>
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
<bean id="springTransactionManager" 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="springTransactionManager" proxy-target-class="true" />
</beans>