<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> <context:annotation-config /> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <tx:jta-transaction-manager /> <context:component-scan base-package="*"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> </context:component-scan> <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:jdbc_oracle_10g.properties</value> </property> </bean> <!-- 引入property文件 end --> <!-- 配置多个数据源 start --> <bean id="test1" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>test1</value> </property> <property name="xaDataSourceClassName"> <value>${test1.jdbc.class}</value> </property> <property name="xaProperties"> <props> <prop key="user">${test1.jdbc.username}</prop> <prop key="password">${test1.jdbc.password}</prop> <prop key="URL">${test1.jdbc.url}</prop> </props> </property> <property name="poolSize"> <value>3</value> </property> </bean> <bean id="test2" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>test2</value> </property> <property name="xaDataSourceClassName"> <value>${test2.jdbc.class}</value> </property> <property name="xaProperties"> <props> <prop key="user">${test2.jdbc.username}</prop> <prop key="password">${test2.jdbc.password}</prop> <prop key="URL">${test2.jdbc.url}</prop> </props> </property> <property name="poolSize"> <value>1</value> </property> </bean> <!-- 配置多个数据源 end --> <!-- 为每个数据源配置相应的SessionFactory start --> <bean id="test1SessionFactoryBean" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="test1" /> <property name="packagesToScan" value="model" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.connection.release_mode">after_transaction</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext </prop> </props> </property> </bean> <bean id="test2SessionFactoryBean" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="test2" /> <property name="packagesToScan" value="model" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.connection.release_mode">after_transaction</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext </prop> </props> </property> </bean> <!-- 为每个数据源配置相应的SessionFactory end --> <!-- 配置动态可切换的sessionFactoryContent start --> <bean id="sessionFactoryContent" class="dao.session.impl.DynamicSessionFactoryImpl"> <property name="defaultTargetSessionFactory" ref="test1SessionFactoryBean" /> <property name="targetSessionFactorys"> <map> <entry value-ref="test1SessionFactoryBean" key="test1" /> <entry value-ref="test2SessionFactoryBean" key="test2" /> </map> </property> </bean> <!-- 配置动态可切换的sessionFactoryContent end --> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="true" /> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout" value="300" /> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="atomikosTransactionManager" /> <property name="userTransaction" ref="atomikosUserTransaction" /> <property name="allowCustomIsolationLevels" value="true" /> </bean> <!-- 配置通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" rollback-for="Exception,RuntimeException,exception.DBException" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> </aop:config> </beans>
以上是关键代码<applicationContext_dao.xml>
附件中有全部关键代码,有需要的童鞋请参考
相关推荐
spring+hibernate+atomikos实现多数据源分布式事务管理
spring+hibernate+jta 分布式事务管理Demo源码(无jar包) 博文链接:https://momoko8443.iteye.com/blog/190994
Spring boot+Atomikos+JTA+Hibernate+MySQL实现分布式事务+多数据源,分别向两个不同的数据里面插入数据同时失败和成功,调用接口方式原理一样。
atomikos实现多数据源支持分布式事务管理(spring、tomcat、JTA) 结合spring 和durid进行配置,
springboot+Atomikos+jpa+mysql的JTA分布式事务实现,本案例涉及到2个数据库,预期结果,在同一个事务中,两个库的状态一致
Atomikos,是一个基于Java的开源事务管理器,提供了事务管理和连接池,不需要应用服务器支持,支持JDBC和JMS事务,能提供对Spring,Hibernate的集成,有兴趣多了解的可以直接参考说明文当,有详细的介绍和使用说明。
使用JTA处理分布式事务 i. 32.1. 使用一个Atomikos事务管理器 ii. 32.2. 使用一个Bitronix事务管理器 iii. 32.3. 使用一个J2EE管理的事务管理器 iv. 32.4. 混合XA和non-XA的JMS连接 v. 32.5. 支持可替代的内嵌事务...