配置两个dataSource
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"> <property name="uniqueResourceName"><value>yw</value></property> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/> <property name="xaProperties"> <props> <prop key="URL">${jdbc.url}</prop> <prop key="user">${jdbc.username}</prop> <prop key="password">${jdbc.password}</prop> </props> </property> <property name="poolSize"><value>1</value></property> <property name="minPoolSize" value="2" /> <property name="maxPoolSize" value="10" /> </bean> <!-- 测试 --> <bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean"> <property name="uniqueResourceName"><value>ds2</value></property> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/> <property name="xaProperties"> <props> <prop key="URL">${jdbc.url2}</prop> <prop key="user">${jdbc.username2}</prop> <prop key="password">${jdbc.password2}</prop> </props> </property> <property name="poolSize"><value>1</value></property> <property name="minPoolSize" value="2" /> <property name="maxPoolSize" value="10" /> </bean>
jdbctemplate配置
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <!-- 测试数据源 --> <bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref local="dataSource2" /> </property> </bean> <bean id="baseDao" class="com.szy.base.BaseDao"> <property name="jdbcTemplate"> <ref bean="jdbcTemplate" /> </property> </bean>
第二个数据源可以用spring注解注入。
public class BaseDao2 { @Resource(name = "jdbcTemplate2") protected JdbcTemplate jdbcTemplate2=null; private int total=0; public void setTotal(int total) { this.total = total; } }
配置事务管理
<!-- 开始配置JTA事务 --> <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</value></property> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager"> <ref local="atomikosTransactionManager"/> </property> <property name="userTransaction"> <ref local="atomikosUserTransaction"/> </property> </bean> <!-- 使用annotation定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
这样就完成了。
仅以记录遇到过的问题。
时间: 2024-11-04 19:06:07