java DB 双数据源和数据库事务配置

2020-12-25 14:17:49 浏览数 (1)

代码语言:javascript复制
<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:applicationContext.properties</value>
			</list>
		</property>
	</bean>
	
	
		<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
		
	<context:component-scan base-package="com.jfpal" >
	</context:component-scan>
	
	<!-- 数据源 -->
	<bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
		<property name="minPoolSize" value="1" />
		<property name="maxPoolSize" value="12" />
		<property name="maxIdleTime" value="1800" />
		<property name="acquireIncrement" value="2" />
		<property name="maxStatements" value="10" />
		<property name="initialPoolSize" value="2" />
		<property name="idleConnectionTestPeriod" value="1800" />
		<property name="acquireRetryAttempts" value="30" />
		<property name="acquireRetryDelay" value="100" />
		<property name="breakAfterAcquireFailure" value="false" />
		<property name="testConnectionOnCheckout" value="false" />
	</bean>
  
  
  <!-- 数据源2 -->
	<bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver2}"></property>
		<property name="jdbcUrl" value="${jdbc.url2}"></property>
		<property name="user" value="${jdbc.username2}"/>
		<property name="password" value="${jdbc.password2}"/>
		<property name="minPoolSize" value="1" />
		<property name="maxPoolSize" value="12" />
		<property name="maxIdleTime" value="1800" />
		<property name="acquireIncrement" value="2" />
		<property name="maxStatements" value="10" />
		<property name="initialPoolSize" value="2" />
		<property name="idleConnectionTestPeriod" value="1800" />
		<property name="acquireRetryAttempts" value="30" />
		<property name="acquireRetryDelay" value="100" />
		<property name="breakAfterAcquireFailure" value="false" />
		<property name="testConnectionOnCheckout" value="false" />
	</bean>
	
	<bean id="dynamicDataSource" class="com.jfpal.framework.dao.DynamicDataSource" >  
    <!-- 通过key-value的形式来关联数据源 -->  
    <property name="targetDataSources">  
        <map>  
            <entry value-ref="dataSourceOne" key="dataSourceOne"></entry>  
            <entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry>  
        </map>  
    </property>  
    <property name="defaultTargetDataSource" ref="dataSourceOne" />  
</bean> 
<bean id="dataSourceInterceptor" class="com.jfpal.framework.interceptor.DataSourceInterceptor">
</bean>
	<aop:config>
		<aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">  
            <aop:pointcut id="switch" expression="execution(* com.jfpal.pmout.payterminal.service.*.*.*(..))" /> 
            <!-- 进入方法前设置为数据源2 -->
            <aop:before pointcut-ref="switch" method="setdataSourceTwo" />
            <!-- 退出方法,设置回数据源1 -->  
            <aop:after pointcut-ref="switch" method="setdataSourceOne"/>
        </aop:aspect>
        
	</aop:config>
	
	<!-- SqlSessionFactory配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dynamicDataSource" />
		<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
		<property name="typeAliasesPackage" value="com.jfpal" />
		<!-- 显式指定Mapper文件位置 -->
		<property name="mapperLocations" value="classpath*:mybatis/**/*Mapper.xml" />
		<property name="configurationProperties">
			<props>
				<prop key="dialect">oracle</prop>
			</props>
		</property>
	</bean>

	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
	
	<!-- ================================事务相关控制=================================================    -->
  <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     
          <property name="dataSource" ref="dynamicDataSource"></property>

    </bean>     
  
  <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="insert*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="select*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="find*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="get*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="save*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="delete*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
                                           
    </tx:attributes>
  </tx:advice>
  

</beans>

0 人点赞