java DB 雙資料來源和資料庫事務配置

執筆記憶的空白發表於2015-09-22
<?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>

相關文章