分布式事物控制
程序员文章站
2022-07-05 13:21:31
...
Spring管理多个数据源概念
首先要做分布式数据处理是需要配置多个数据源的,以下是没有事物控制的多个数据源
<!--数据源-->
<bean name="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据连接信息 -->
<property name="jdbcUrl" value="${jdbc.url1}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean name="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据连接信息 -->
<property name="jdbcUrl" value="${jdbc.url2}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--JDBC-->
<bean name="JDBCTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource1"></property>
</bean>
<bean name="JDBCTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource2"></property>
</bean>
2.通过atomikos控制分布式事物
-
添加依赖
<dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.atteo.moonshine</groupId> <artifactId>atomikos</artifactId> <version>1.2</version> </dependency>
-
添加数据源
<!--数据源--> <bean id="dataSource1" class="com.atomikos.jdbc.AtomikosDataSourceBean" destroy-method="close"> <property name="uniqueResourceName" value="dataSource1"></property> <!--驱动--> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/> <property name="xaProperties"> <props> <prop key="user">${jdbc.username}</prop> <prop key="password">${jdbc.password}</prop> <prop key="URL">${jdbc.url1}</prop> </props> </property> </bean> <bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean" destroy-method="close"> <property name="uniqueResourceName" value="dataSource2"></property> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/> <property name="xaProperties"> <props> <prop key="user">${jdbc.username}</prop> <prop key="password">${jdbc.password}</prop> <prop key="URL">${jdbc.url2}</prop> </props> </property> </bean>
-
配置atomikos事物管理器
<!--配置atomikos事物管理器--> <bean id="UserTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"/> <bean id="UserTransactionManagerImp" class="com.atomikos.icatch.jta.UserTransactionImp"/>
-
配置spring事物管理器
<!--Spring jta事务管理器--> <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="userTransactionManager"/> <property name="userTransaction" ref="userTransactionManagerImp"/> </bean>
整体配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config/>
<!--扫描包-->
<context:component-scan base-package="xin.liuyang.dao,xin.liuyang.biz"/>
<!--扫描配置文件-->
<context:property-placeholder location="classpath:properties/*.properties"/>
<!--数据源-->
<bean id="dataSource1" class="com.atomikos.jdbc.AtomikosDataSourceBean" destroy-method="close">
<property name="uniqueResourceName" value="dataSource1"></property>
<!--驱动-->
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
<property name="xaProperties">
<props>
<prop key="user">${jdbc.username}</prop>
<prop key="password">${jdbc.password}</prop>
<prop key="URL">${jdbc.url1}</prop>
</props>
</property>
</bean>
<bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean" destroy-method="close">
<property name="uniqueResourceName" value="dataSource2"></property>
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
<property name="xaProperties">
<props>
<prop key="user">${jdbc.username}</prop>
<prop key="password">${jdbc.password}</prop>
<prop key="URL">${jdbc.url2}</prop>
</props>
</property>
</bean>
<!--配置atomikos事物管理器-->
<bean id="userTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"/>
<bean id="userTransactionManagerImp" class="com.atomikos.icatch.jta.UserTransactionImp"/>
<!--Spring jta事务管理器-->
<bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="userTransactionManager"/>
<property name="userTransaction" ref="userTransactionManagerImp"/>
</bean>
<!--事物管理驱动-->
<tx:annotation-driven transaction-manager="jtaTransactionManager"/>
<!--JDBC-->
<bean name="JDBCTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource1"></property>
</bean>
<bean name="JDBCTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource2"></property>
</bean>
</beans>
下一篇: 获取当前线程堆栈