zoukankan      html  css  js  c++  java
  • 多数据源源路由方案

    数据源配置

    配置 parentDataSource 的父bean.再配置多个数据源继承这个父bean,对driverClass,url,username,password,等数据源连接参数进行各自的重写。例如 mySqlDataSource ,在 DataSources bean中注入所有要切换的数据源,并且设置默认的数据源。

    也可以使用bean的方式进行配置

     1 <bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
     2     </bean>  
     3       
     4     <bean id="mySqlDataSource" parent="parentDataSource">  
     5         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
     6         <property name="url" value="jdbc:mysql://localhost:3306/test"></property>  
     7         <property name="username" value="root"></property>  
     8         <property name="password" value="root"></property>  
     9     </bean>  
    10       
    11     <bean id="oracleDataSource" parent="parentDataSource">  
    12         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>  
    13         <property name="url" value="jdbc:oracle:thin:@10.16.17.40:1531:addb"></property>  
    14         <property name="username" value="trac"></property>  
    15         <property name="password" value="trac"></property>  
    16     </bean>  
    17       
    18     <bean id="dataSource" class="com.trac.dao.datasource.DataSources">  
    19         <property name="targetDataSources">  
    20             <map key-type="java.lang.String">  
    21                 <entry value-ref="mySqlDataSource" key="MYSQL"></entry>  
    22                 <entry value-ref="oracleDataSource" key="ORACLE"></entry>  
    23             </map>  
    24         </property>  
    25         <property name="defaultTargetDataSource" ref="oracleDataSource"></property>  
    26     </bean>  
    27   
    28     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    29         <property name="dataSource" ref="dataSource" />  
    30     </bean>  
    31   
    32     <!-- 创建SqlSessionFactory,同时指定数据源和mapper -->  
    33     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    34         <property name="dataSource" ref="dataSource" />  
    35         <property name="mapperLocations" value="classpath*:com/trac/ibatis/dbcp/*.xml" />  
    36     </bean>  
    37   
    38     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
    39         <constructor-arg index="0" ref="sqlSessionFactory" />  
    40     </bean>  
    41   
    42     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    43         <property name="basePackage" value="com.trac.dao" />  
    44     </bean>  

    数据源常量

    定义数据源的标识, 和applicationContext.xml中 DataSources 的 targetDataSources 的key对应  

    1 public class DataSourceInstances{  
    2     public static final String MYSQL="MYSQL";  
    3     public static final String ORACLE="ORACLE";  
    4 }  

    数据源路由

    配置于applicationContext 中,线程局部变量ThreadLocal contextHolder 保存当前需要的数据源类型,当 DataSourceSwitch.setDataSourceType(DataSourceInstances.XXX) 保存当前需要的数据源类型的时候,DataSources 会从当前线程中查找线程变量的数据源类型,从而决定使用何种数据源

     1 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;  
     2   
     3 public class DataSources extends AbstractRoutingDataSource{  
     4   
     5     @Override  
     6     protected Object determineCurrentLookupKey() {  
     7         return DataSourceSwitch.getDataSourceType();  
     8     }  
     9   
    10 }  
    public class DataSourceSwitch{  
        private static final ThreadLocal contextHolder=new ThreadLocal();  
          
        public static void setDataSourceType(String dataSourceType){  
            contextHolder.set(dataSourceType);  
        }  
          
        public static String getDataSourceType(){  
            return (String) contextHolder.get();  
        }  
          
        public static void clearDataSourceType(){  
            contextHolder.remove();  
        }  
    } 

    参考: https://www.iteye.com/blog/leoyy-1624704

    感谢原作者

  • 相关阅读:
    Hive的安装
    ubuntu下能ping通ssh不通的解决思路
    ubuntu下pig报错ERROR 2999: Unexpected internal error. Failed to create DataStorage的解决
    Ubuntu系统中各种文件颜色的含义
    Hbase建表时遇到的问题This could be a sign that the server has too many connections
    Python
    Python
    Python
    Python
    Python
  • 原文地址:https://www.cnblogs.com/daixianjun/p/datasource-swith.html
Copyright © 2011-2022 走看看