1.在spring中,一个transactionManager不能对应多个数据源,如果配置两个数据源,同时还要在service层保证两个数据库的事务.就需要配置两个事物管理器,定义两个事务级别,定义两个切面.(需要保证两个数据源配置的事务级别,切面一致).
1.数据库一
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "
default-lazy-init="true">
<!-- 自动扫描组件,这里要把controler下面的 controller去除, 他们是在springmvc-servlet.xml中配置的,如果不去除会影响事务管理的。 -->
<context:component-scan base-package="com.zhitengda.*">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="mainDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 测试库 -->
<property name="url" value="jdbc:oracle:thin:@121.201.37.4:7718:ztddb" />
<property name="username" value="yfky" />
<property name="password" value="Yfky_%2017!$%" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="20" />
<property name="maxWait" value="10000" />
<property name="minIdle" value="20" />
<property name="timeBetweenEvictionRunsMillis" value="3000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 1 FROM DUAL" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
</bean>
<bean id="mainSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:/mybatis/mapper-config.xml"></property>
<property name="dataSource" ref="mainDataSource" />
<property name="mapperLocations" value="classpath:com/zhitengda/*/eneity/sqlmap/*.xml" />
</bean>
<!-- 采用spring与mybatis整合的第一种方法 -->
<bean id="mainSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="mainSqlSessionFactory" />
</bean>
<!-- 事务管理开始 -->
<bean id="mainTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="mainDataSource" />
</property>
</bean>
<!-- 设置监控的方法事务级别 -->
<tx:advice id="mainTxAdvice" transaction-manager="mainTransactionManager">
<tx:attributes>
<!-- REQUIRED: 第一种事务声明方式,需要事务
SUPPORTS:第四种声明方式,不需要事务
Service层方法必须严格按照下面的规范命名 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="count*" propagation="SUPPORTS" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="list*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 设置监控的方所在路径 -->
<aop:config>
<aop:pointcut id="mainMethod"
expression="execution(* com.zhitengda.*.service.impl..*.*(..))" />
<aop:advisor advice-ref="mainTxAdvice" pointcut-ref="mainMethod" />
</aop:config>
<!-- 事务管理结束 -->
</beans>
2.数据库二
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "
default-lazy-init="true">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 测试库 -->
<property name="url" value="jdbc:oracle:thin:@192.168.3.241:1521:lbtest" />
<property name="username" value="jgsd" />
<property name="password" value="jgsd_1019" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="20" />
<property name="maxWait" value="10000" />
<property name="minIdle" value="20" />
<property name="timeBetweenEvictionRunsMillis" value="3000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 1 FROM DUAL" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
</bean>
<!-- 配置sqlSessionFactory工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations"
value="classpath:com/zhitengda/*/eneity/sqlmap/*.xml" />
<property name="configLocation" value="classpath:mybatis/mapper-config2.xml" />
</bean>
<!-- 采用spring与mybatis整合的第一种方法 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- 事务管理开始 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- REQUIRED: 第一种事务声明方式,需要事务
SUPPORTS:第四种声明方式,不需要事务
Service层方法必须严格按照下面的规范命名 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="count*" propagation="SUPPORTS" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="list*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txPointcut"
expression="execution(* com.zhitengda.*.service.impl..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
<!-- 事务管理结束 -->
</beans>
只要保证数据源的对应的切面,事务隔离级别一致,就可以保证在service层的同一个方法内,对两个数据库的操作在同一个事务.