使用JTA实现跨库事务

本文是基于atomikos实现的JTA跨库事务
示例中的数据访问层为Hibernate,可替换为喜欢的任意的方式。
具体使用比较简单,只是需要在配置上做一些修改
具体测试源码见附件,其中核心配置如下:

<context:component-scan base-package="com.ajita.*" />
<tx:annotation-driven />
<tx:jta-transaction-manager />

<bean id="dataSourceA" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>DataSourceA</value>
</property>
<property name="xaDataSourceClassName">
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
<property name="xaProperties">
<props>
<prop key="databaseName">test</prop>
<prop key="serverName">127.0.0.1</prop>
<prop key="port">3306</prop>
<prop key="user">root</prop>
<prop key="password">123456</prop>
<prop key="url">jdbc:mysql://127.0.0.1:3306/test</prop>
</props>
</property>
<property name="minPoolSize">
<value>1</value>
</property>
</bean>

<bean id="dataSourceB" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>DataSourceB</value>
</property>
<property name="xaDataSourceClassName">
<value>net.sourceforge.jtds.jdbcx.JtdsDataSource</value>
</property>
<property name="xaProperties">
<props>
<prop key="databaseName">SimpleDB</prop>
<prop key="serverName">127.0.0.1</prop>
<prop key="portNumber">1434</prop>
<prop key="user">sa</prop>
<prop key="password">123456</prop>
</props>
</property>
<property name="minPoolSize">
<value>1</value>
</property>
</bean>

<bean id="entityManagerFactoryA"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation">
<value>classpath*:persistence.xml</value>
</property>
<property name="persistenceUnitName" value="PersistenceUnitA" />
<property name="dataSource" ref="dataSourceA" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</bean>
</property>
</bean>

<bean id="entityManagerFactoryB"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation">
<value>classpath*:persistence.xml</value>
</property>
<property name="persistenceUnitName" value="PersistenceUnitB" />
<property name="dataSource" ref="dataSourceB" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
</bean>
</property>
</bean>

<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<property name="forceShutdown" value="false" />
</bean>

<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.J2eeUserTransaction">
<property name="transactionTimeout" value="300" />
</bean>

<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager"
depends-on="atomikosTransactionManager,atomikosUserTransaction">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
<property name="allowCustomIsolationLevels" value="true" />
</bean>


有不清楚的,欢迎留言交流~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值