关于双数据源的切换问题

之前让配置一个双数据源,然后在不同的地方调用不同的数据源问题,通过上网查询最终解决了问题,但是在切换数据源的地方卡了一下,在这里主要阐述一下双数据的切换(配置文件方式切换)

先看网上的一个例子(引用地址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>

于是就大功告成了,当然,可能感觉比较粗糙,我也只是拾人牙慧而已。但若能给人提供下思路也是极好的。。

posted on 2016-06-29 14:29  孤王就是朕  阅读(4956)  评论(2编辑  收藏  举报