使用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>


有不清楚的,欢迎留言交流~

你可能感兴趣的:(jta,atomikos,跨库事务)