Spring框架整合多数据源 Mysql+oracle

原项目是mysql数据库,现业务需求新加oracle数据源

引oracle驱动jar包ojdbc6.jar  数据连接池换为druid  新加jar包druid-1.0.15.jar

applicationContext.xml文件

<!-- 引入jdbc配置文件 -->
    <context:property-placeholder location="classpath*:config.properties" />
    
    <bean id="dataSourceMysql" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${db.driverClass}"/>
         <property name="url" value="jdbc:mysql://localhost:3306/mulinsen?noAccessToProcedureBodies=true"/>
        <property name="username" value="${db.user}"/>
        <property name="password" value="${db.password}"/>
        
        <property name="maxActive" value="20" />  
        <property name="initialSize" value="1" />  
        <property name="maxWait" value="60000" />  
        <property name="maxIdle" value="15" />  
        <property name="minIdle" value="5" />  
        <property name="removeAbandoned" value="true" />  
        <property name="removeAbandonedTimeout" value="180" />  
        <property name="connectionProperties">  
            <value>clientEncoding=UTF-8</value>  
        </property>
    </bean>
    <bean id="dataSourceMysqlTest" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${db.driverClass}"/>
         <property name="url" value="jdbc:mysql://localhost:3306/mulinsen_erp?noAccessToProcedureBodies=true"/>
        <property name="username" value="${db.user}"/>
        <property name="password" value="${db.password}"/>
        
        <property name="maxActive" value="20" />  
        <property name="initialSize" value="1" />  
        <property name="maxWait" value="60000" />  
        <property name="maxIdle" value="15" />  
        <property name="minIdle" value="5" />  
        <property name="removeAbandoned" value="true" />  
        <property name="removeAbandonedTimeout" value="180" />  
        <property name="connectionProperties">  
            <value>clientEncoding=UTF-8</value>  
        </property>
    </bean>
        
    <!-- ERP数据源 -->
    <bean id="dataSourceOracle" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"  destroy-method="close">  
        <!-- 基本属性 url、user、password -->  
        <property name="driverClassName" value="${db.driverClassErp}"/>
         <property name="url" value="${db.urlErp}"/>
        <property name="username" value="${db.userErp}"/>
        <property name="password" value="${db.passwordErp}"/>
        <!-- 配置初始化大小、最小、最大 -->  
        <property name="initialSize" value="1" />  
        <property name="minIdle" value="1" />  
        <property name="maxActive" value="20" />  
        <!-- 配置获取连接等待超时的时间 -->  
        <property name="maxWait" value="60000" />  
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
        <property name="timeBetweenEvictionRunsMillis" value="60000" />  
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
        <property name="minEvictableIdleTimeMillis" value="300000" />  
        <property name="validationQuery" value="SELECT 'x' FROM DUAL" />  
        <property name="testWhileIdle" value="true" />  
        <property name="testOnBorrow" value="false" />  
        <property name="testOnReturn" value="false" />  
        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->  
        <property name="poolPreparedStatements" value="true" />  
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  
        <!-- 配置监控统计拦截的filters -->  
        <property name="filters" value="stat" /> 
    </bean>
    <bean id="dataSource" class="net.aykj.datasource.DataSourceRouter"> 
        <description>多数据源路由</description>     
        <property name="targetDataSources">      
            <map key-type="java.lang.String">      
                <entry value-ref="dataSourceMysql" key="mysql"></entry>      
                <entry value-ref="dataSourceOracle" key="oracle"></entry>      
                <entry value-ref="dataSourceMysqlTest" key="mysqltest"></entry>      
            </map>      
        </property>  
        <!-- 默认使用dataSourceMysql的数据源 -->    
        <property name="defaultTargetDataSource" ref="dataSourceMysql"></property>         
    </bean>  
DataSourceRouter.java
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DataSourceRouter  extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        // TODO Auto-generated method stub

        return DataSourceTypeManager.get();
    }

}
DataSourceTypeManager.java(为了避免多个线程间造成并发问题,用了线程本地变量)
public class DataSourceTypeManager {

    //
    public static final String MySql = "mysql";
    public static final String MySqlTest = "mysqltest";
    //public static final String Oracle = "oracle";
    public static final String Oracle = "mysqltest";//先用于测试  日后直接换到orcale的数据源


    private static final ThreadLocal<String> dataSourceKey = new ThreadLocal<String>(){
       /* @Override
        protected String initialValue(){
            return DS_SUNGBIO;
        }*/
    };
    
    public static void setDataSourceKey(String dataSource) {
        dataSourceKey.set(dataSource);
    }
    public static String get(){
        return dataSourceKey.get();
    }
    public static void set(String dataSourceType){
        dataSourceKey.set(dataSourceType);
    }

    public static void reset(){
        dataSourceKey.set(MySql);
    }

    public static void cleanDataSource(){
        dataSourceKey.remove();
    }
}

使用方法:

使用前,手动写:

DataSourceTypeManager.setDataSourceKey(DataSourceTypeManager.Oracle);

来回切换数据源,或者返回默认可以

DataSourceTypeManager.reset();

原博客:https://www.cnblogs.com/grey-wolf/p/6962869.html

posted @ 2019-06-20 18:17  李晓梦  阅读(1096)  评论(0编辑  收藏  举报