原文:http://www.blogjava.net/pdw2009/archive/2008/05/25/202725.html
applicationContext.xml配置
<!--Atomikos JTA 事务 begin-->
<bean id="dataSource1" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>mysql/main</value>
</property>
<property name="xaDataSourceClassName">
<!-- 使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)-->
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
<property name="xaDataSourceProperties">
<value>URL=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8;user=root;password=123456</value>
</property>
<property name="exclusiveConnectionMode">
<value>true</value>
</property>
<property name="connectionPoolSize">
<value>3</value>
</property>
<property name="validatingQuery">
<value>SELECT 1</value>
</property>
</bean>
<bean id="dataSource2" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>mysql/secondary</value>
</property>
<property name="xaDataSourceClassName">
<!-- 使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)-->
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
<property name="xaDataSourceProperties">
<value>URL=jdbc:mysql://localhost:3306/aotsf?useUnicode=true&characterEncoding=utf-8;user=root;password=123456</value>
</property>
<property name="exclusiveConnectionMode">
<value>true</value>
</property>
<property name="connectionPoolSize">
<value>3</value>
</property>
<property name="validatingQuery">
<value>SELECT 1</value>
</property>
</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>
<bean id="atomikosTransactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="springTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<bean id="jdbcBookDAO" class="dao.jdbc.BookDAO">
<property name="dataSource">
<ref bean="dataSource1"/>
</property>
</bean>
<bean id="jdbcTestDAO" class="dao.jdbc.TestDAO">
<property name="dataSource">
<ref bean="dataSource2"/>
</property>
</bean>
<bean id="atomikosJTAServiceDemo" parent="atomikosTransactionProxy">
<property name="target">
<bean class="service.AtomikosJTAServiceDemo">
<property name="jdbcBookDAO">
<ref bean="jdbcBookDAO"/>
</property>
<property name="jdbcTestDAO">
<ref bean="jdbcTestDAO"/>
</property>
</bean>
</property>
</bean>
<!--Atomikos JTA 事务 end -->
applicationContext.xml配置
<!--Atomikos JTA 事务 begin-->
<bean id="dataSource1" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>mysql/main</value>
</property>
<property name="xaDataSourceClassName">
<!-- 使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)-->
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
<property name="xaDataSourceProperties">
<value>URL=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8;user=root;password=123456</value>
</property>
<property name="exclusiveConnectionMode">
<value>true</value>
</property>
<property name="connectionPoolSize">
<value>3</value>
</property>
<property name="validatingQuery">
<value>SELECT 1</value>
</property>
</bean>
<bean id="dataSource2" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName">
<value>mysql/secondary</value>
</property>
<property name="xaDataSourceClassName">
<!-- 使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)-->
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
<property name="xaDataSourceProperties">
<value>URL=jdbc:mysql://localhost:3306/aotsf?useUnicode=true&characterEncoding=utf-8;user=root;password=123456</value>
</property>
<property name="exclusiveConnectionMode">
<value>true</value>
</property>
<property name="connectionPoolSize">
<value>3</value>
</property>
<property name="validatingQuery">
<value>SELECT 1</value>
</property>
</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>
<bean id="atomikosTransactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="springTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<bean id="jdbcBookDAO" class="dao.jdbc.BookDAO">
<property name="dataSource">
<ref bean="dataSource1"/>
</property>
</bean>
<bean id="jdbcTestDAO" class="dao.jdbc.TestDAO">
<property name="dataSource">
<ref bean="dataSource2"/>
</property>
</bean>
<bean id="atomikosJTAServiceDemo" parent="atomikosTransactionProxy">
<property name="target">
<bean class="service.AtomikosJTAServiceDemo">
<property name="jdbcBookDAO">
<ref bean="jdbcBookDAO"/>
</property>
<property name="jdbcTestDAO">
<ref bean="jdbcTestDAO"/>
</property>
</bean>
</property>
</bean>
<!--Atomikos JTA 事务 end -->