之前讓配置一個雙數據源,然后在不同的地方調用不同的數據源問題,通過上網查詢最終解決了問題,但是在切換數據源的地方卡了一下,在這里主要闡述一下雙數據的切換(配置文件方式切換)
先看網上的一個例子(引用地址http://blog.csdn.net/wangpeng047/article/details/8866239 ,里面還有demo下載 )
<?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-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <context:annotation-config /> <aop:aspectj-autoproxy /> <context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <context:property-placeholder location="classpath:com/resources/datasource.properties" /> <bean id="dataSourceMySql" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${mysql.jdbc.driverClass}" /> <property name="jdbcUrl" value="${mysql.jdbc.url}" /> <property name="user" value="${mysql.jdbc.user}" /> <property name="password" value="${mysql.jdbc.password}" /> <property name="initialPoolSize" value="${mysql.jdbc.initialPoolSize}" /> <property name="minPoolSize" value="${mysql.jdbc.minPoolSize}" /> <property name="maxPoolSize" value="${mysql.jdbc.maxPoolSize}" /> <property name="checkoutTimeout" value="${mysql.jdbc.checkoutTimeout}" /> <property name="idleConnectionTestPeriod" value="${mysql.jdbc.idleConnectionTestPeriod}" /> <property name="maxIdleTime" value="${mysql.jdbc.maxIdleTime}" /> <property name="maxStatements" value="${mysql.jdbc.maxStatements}" /> <property name="testConnectionOnCheckout" value="${mysql.jdbc.testConnectionOnCheckout}" /> </bean> <bean id="dataSourceOracle" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${oracle.jdbc.driverClass}" /> <property name="jdbcUrl" value="${oracle.jdbc.url}" /> <property name="user" value="${oracle.jdbc.user}" /> <property name="password" value="${oracle.jdbc.password}" /> <property name="initialPoolSize" value="${oracle.jdbc.initialPoolSize}" /> <property name="minPoolSize" value="${oracle.jdbc.minPoolSize}" /> <property name="maxPoolSize" value="${oracle.jdbc.maxPoolSize}" /> <property name="checkoutTimeout" value="${oracle.jdbc.checkoutTimeout}" /> <property name="idleConnectionTestPeriod" value="${oracle.jdbc.idleConnectionTestPeriod}" /> <property name="maxIdleTime" value="${oracle.jdbc.maxIdleTime}" /> <property name="maxStatements" value="${oracle.jdbc.maxStatements}" /> <property name="testConnectionOnCheckout" value="${oracle.jdbc.testConnectionOnCheckout}" /> </bean> <bean id="dataSource" class="com.core.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="dataSourceMySql" value-ref="dataSourceMySql" /> <entry key="dataSourceOracle" value-ref="dataSourceOracle" /> </map> </property> <property name="defaultTargetDataSource" ref="dataSourceMySql" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:com/resources/hibernate.cfg.xml" /> <property name="packagesToScan"> <list> <value>com.pojo.po</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <aop:config> <aop:pointcut id="serviceMethods" expression="execution(* com.service..*.*(..))" /> <aop:advisor advice-ref="txadvice" pointcut-ref="serviceMethods" /> </aop:config> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="get*" read-only="true" propagation="SUPPORTS" /> <tx:method name="find*" read-only="true" propagation="SUPPORTS" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <bean id="dataSourceInterceptor" class="com.core.DataSourceInterceptor" /> <aop:config> <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor"> <aop:pointcut id="dsMysql" expression="execution(* com.controller.mysql.*.*(..))" /> <aop:pointcut id="dsOracle" expression="execution(* com.controller.oracle.*.*(..))" /> <aop:before method="setdataSourceMysql" pointcut-ref="dsMysql"/> <aop:before method="setdataSourceOracle" pointcut-ref="dsOracle"/> </aop:aspect> </aop:config> </beans>
最下方即為雙數據源切換,這個數據源切換沒有問題,我碰到的為一個文件夾下的需要切換到需要的數據源,其他的全部為另外一個,我在想上面配置中defaultTargetDataSource,是不是就是在不切換的時候為默認的於是,我就把切換地方改成
<aop:config> <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor"> <aop:pointcut id="dsOracle" expression="execution(* com.controller.oracle.*.*(..))" /> <aop:before method="setdataSourceOracle" pointcut-ref="dsOracle"/> </aop:aspect> </aop:config>
這樣,會不會是需要oracle數據源的時候就切換oracle,調用其他的就默認到了mysql那個庫呢,結果運行的時候發現沒有,可能是其他原因沒有找到吧,於是我就在
expression="execution(這個表達式上面解決問題,比如除了上面表達式的文件下其他的都切換,類似這樣
<aop:config> <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor"> <aop:pointcut id="dsMysql" expression="not execution(* com.controller.oracle.*.*(..))" /> <aop:pointcut id="dsOracle" expression="execution(* com.controller.oracle.*.*(..))" /> <aop:before method="setdataSourceMysql" pointcut-ref="dsMysql"/> <aop:before method="setdataSourceOracle" pointcut-ref="dsOracle"/> </aop:aspect> </aop:config>
結果網上搜了一大堆,什么! nor not,都用了,但是仍然不好使,當然我覺得思路肯定是對的,至於原因還不知道。。於是換個方式,在com.core.DataSourceInterceptor中添加remove數據源,讓用完數據源就remove掉,那樣你總能用上defaultTargetDataSource吧,,然后果斷好使了,當然寫這個就是給大家提供思路,或許有其他解決方法呢。好吧上代碼先,先看提供的兩個切換類,配置中用到的com.core.DataSourceInterceptor
package com.core; import org.aspectj.lang.JoinPoint; public class DataSourceInterceptor { public void setdataSourceMysql(JoinPoint jp) { DatabaseContextHolder.setCustomerType("dataSourceMySql"); } public void setdataSourceOracle(JoinPoint jp) { DatabaseContextHolder.setCustomerType("dataSourceOracle"); } }
該類所調用的類 DatabaseContextHolder
package com.core; public class DatabaseContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); public static void setCustomerType(String customerType) { contextHolder.set(customerType); } public static String getCustomerType() { return contextHolder.get(); } public static void clearCustomerType() { contextHolder.remove(); } }
於是就把DataSourceInterceptor中加個方法調用DatabaseContextHolder的clearCustomerType方法
於是有
package com.core; import org.aspectj.lang.JoinPoint; public class DataSourceInterceptor { public void setdataSourceMysql(JoinPoint jp) { DatabaseContextHolder.setCustomerType("dataSourceMySql"); } public void setdataSourceOracle(JoinPoint jp) { DatabaseContextHolder.setCustomerType("dataSourceOracle"); } public void removedataSource(JoinPoint jp){ DatabaseContextHolder.clearCustomerType(); } }
然后xml配置改成
<aop:config> <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor"> <aop:pointcut id="dsOracle" expression="execution(* com.controller.oracle.*.*(..))" /> <aop:before method="setdataSourceOracle" pointcut-ref="dsOracle"/> <aop:before method="removedataSource" pointcut-ref="dsOracle"/> </aop:aspect> </aop:config>
於是就大功告成了,當然,可能感覺比較粗糙,我也只是拾人牙慧而已。但若能給人提供下思路也是極好的。。