spring能不能对多个数据库进行事务管理
1个回答
2016-02-26
展开全部
可以,Spring的事务处理能力是最综合完整的,对编程式、声明式均进行了支持,而且可以在不使用应用服务器的情况下兼容应用服务器事务。下面的是一个分布式的声明事务,访问多个数据源的配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<!-- Configurer that replaces ${...} placeholders with values from a properties
file -->
<!-- (in this case, JDBC-related settings for the dataSource definition
below) -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:spring/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSourceMysql" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="${mysql.uniqueResourceName}" />
<property name="xaDataSourceClassName" value="${mysql.xaDataSourceClassName}" />
<property name="xaProperties">
<props>
<prop key="user">${mysql.user}</prop>
<prop key="password">${mysql.password}</prop>
<prop key="URL">${mysql.url}</prop>
</props>
</property>
<property name="poolSize" value="${mysql.poolSize}" />
</bean>
<bean id="dataSourceOracle" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="${oracle.uniqueResourceName}" />
<property name="xaDataSourceClassName" value="${oracle.xaDataSourceClassName}" />
<property name="xaProperties">
<props>
<prop key="user">${oracle.user}</prop>
<prop key="password">${oracle.password}</prop>
<prop key="URL">${oracle.url}</prop>
</props>
</property>
<property name="poolSize" value="${oracle.poolSize}" />
</bean>
<!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<!-- when close is called, should we force transactions to terminate or
not? -->
<property name="forceShutdown" value="${transactionManager.forceShutdown}" />
</bean>
<!-- Also use Atomikos UserTransactionImp, needed to configure Spring -->
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="${transactionManager.transactionTimeout}" />
</bean>
<!-- Configure the Spring framework to use JTA transactions from Atomikos -->
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<ref bean="atomikosTransactionManager" />
</property>
<property name="userTransaction">
<ref bean="atomikosUserTransaction" />
</property>
</bean>
<bean id="txManager"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED, +AuaException</prop>
</props>
</property>
</bean>
<bean id="studentOracleDao" class="com.aua.dao.oracle.StudentDao" />
<bean id="studentMysqlDao" class="com.aua.dao.mysql.StudentDao" />
<bean id="studentService" parent="txManager">
<property name="target">
<bean class="com.aua.service.impl.StudentService" />
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<!-- Configurer that replaces ${...} placeholders with values from a properties
file -->
<!-- (in this case, JDBC-related settings for the dataSource definition
below) -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:spring/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSourceMysql" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="${mysql.uniqueResourceName}" />
<property name="xaDataSourceClassName" value="${mysql.xaDataSourceClassName}" />
<property name="xaProperties">
<props>
<prop key="user">${mysql.user}</prop>
<prop key="password">${mysql.password}</prop>
<prop key="URL">${mysql.url}</prop>
</props>
</property>
<property name="poolSize" value="${mysql.poolSize}" />
</bean>
<bean id="dataSourceOracle" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="${oracle.uniqueResourceName}" />
<property name="xaDataSourceClassName" value="${oracle.xaDataSourceClassName}" />
<property name="xaProperties">
<props>
<prop key="user">${oracle.user}</prop>
<prop key="password">${oracle.password}</prop>
<prop key="URL">${oracle.url}</prop>
</props>
</property>
<property name="poolSize" value="${oracle.poolSize}" />
</bean>
<!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<!-- when close is called, should we force transactions to terminate or
not? -->
<property name="forceShutdown" value="${transactionManager.forceShutdown}" />
</bean>
<!-- Also use Atomikos UserTransactionImp, needed to configure Spring -->
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="${transactionManager.transactionTimeout}" />
</bean>
<!-- Configure the Spring framework to use JTA transactions from Atomikos -->
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<ref bean="atomikosTransactionManager" />
</property>
<property name="userTransaction">
<ref bean="atomikosUserTransaction" />
</property>
</bean>
<bean id="txManager"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED, +AuaException</prop>
</props>
</property>
</bean>
<bean id="studentOracleDao" class="com.aua.dao.oracle.StudentDao" />
<bean id="studentMysqlDao" class="com.aua.dao.mysql.StudentDao" />
<bean id="studentService" parent="txManager">
<property name="target">
<bean class="com.aua.service.impl.StudentService" />
</property>
</bean>
</beans>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |