刚好项目要用到多数据源,在网上查找了很多资料,花了几天的时间对spring、jta 、atomikos、hibernate进行多数据源事务管理的配置。
也可以直接放在tomcat中运行,不过要用ws调用,因为没有做界面,ws入口在controller包里
下面是jta配置文件:
001 | <context:annotation-config /> |
003 | <context:component-scan base-package="com.rongli.service,com.rongli.dao,com.rongli.controller" /> |
005 | <tx:annotation-driven transaction-manager="springJTATransactionManager" proxy-target-class="true"/> |
007 | <import resource="ws.cxf.xml"/> |
010 | <bean id="propertyConfigurer" |
011 | class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> |
012 | <property name="locations"> |
014 | <value>classpath:jta.jdbc.properties</value> |
020 | <bean id="abstractXADataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" |
021 | init-method="init" destroy-method="close" abstract="true"> |
022 | <property name="xaDataSourceClassName" |
023 | value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" /> |
024 | <property name="poolSize" value="10" /> |
025 | <property name="minPoolSize" value="10" /> |
026 | <property name="maxPoolSize" value="30" /> |
028 | <property name="borrowConnectionTimeout" value="60" /> |
031 | <property name="reapTimeout" value="20000" /> |
033 | <property name="maxIdleTime" value="60" /> |
035 | <property name="maintenanceInterval" value="60" /> |
037 | <property name="loginTimeout" value="60" /> |
038 | <property name="testQuery"> |
039 | <value>select 1</value> |
044 | <bean id="rlc_cus" parent="abstractXADataSource"> |
045 | <property name="uniqueResourceName" value="mysql/rlc_cus" /> |
046 | <property name="xaProperties"> |
048 | <prop key="URL">${jdbc.rlc_cus.properties}</prop> |
049 | <prop key="user">${jdbc.rlc.user}</prop> |
050 | <prop key="password">${jdbc.rlc.password}</prop> |
051 | <prop key="pinGlobalTxToPhysicalConnection">true</prop> |
057 | <bean id="rlc_sys" parent="abstractXADataSource"> |
058 | <property name="uniqueResourceName" value="mysql/rlc_sys" /> |
059 | <property name="xaProperties"> |
061 | <prop key="URL">${jdbc.rlc_sys.properties}</prop> |
062 | <prop key="user">${jdbc.rlc.user}</prop> |
063 | <prop key="password">${jdbc.rlc.password}</prop> |
064 | <prop key="pinGlobalTxToPhysicalConnection">true</prop> |
070 | <bean id="baseSessionFactory" |
071 | class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" |
073 | <property name="hibernateProperties"> |
075 | <prop key="hibernate.dialect">${hibernate.dialect}</prop> |
076 | <prop key="hibernate.show_sql">${hibernate.format_sql}</prop> |
077 | <prop key="hibernate.format_sql">${hibernate.show_sql}</prop> |
078 | <prop key="javax.persistence.validation.mode">none</prop> |
082 | <property name="packagesToScan" value="com.rongli.bean"></property> |
087 | <bean id="cusSessionFactory" parent="baseSessionFactory"> |
088 | <property name="dataSource" ref="rlc_cus" /> |
091 | <bean id="sysSessionFactory" parent="baseSessionFactory"> |
092 | <property name="dataSource" ref="rlc_sys" /> |
096 | <bean id="atomikosTransactionManager" |
097 | class="com.atomikos.icatch.jta.UserTransactionManager" |
098 | init-method="init" destroy-method="close"> |
100 | <property name="forceShutdown"> |
104 | <bean id="atomikosUserTransaction" |
105 | class="com.atomikos.icatch.jta.UserTransactionImp"> |
106 | <property name="transactionTimeout"> |
112 | <bean id="springJTATransactionManager" |
113 | class="org.springframework.transaction.jta.JtaTransactionManager"> |
114 | <property name="transactionManager"> |
115 | <ref bean="atomikosTransactionManager" /> |
117 | <property name="userTransaction"> |
118 | <ref bean="atomikosUserTransaction" /> |
123 | <bean id="systemServiceImpl" |
124 | class="com.rongli.service.impl.SystemServiceImpl"> |
运行截图:
原2个数据库中都没有数据,图如下:


具体代码:


代码运行成功截图:
成功后数据库(乱码请忽略):


代码故意抛出异常:

代码运行失败:

失败后2个数据库中没有插入(事务回滚了):
