c3p0如何配置多数据源的解决方法

来源:互联网 发布:讲你知有没有国语 编辑:程序博客网 时间:2024/06/06 14:00

c3p0如何配置多数据源的解决方法

一、问题描述:

    项目开发时,用c3p0管理数据连接。但是项目需要动态连接两个以上的数据库,如何用c3p0配置多数据源。

二、解决方法:

    使用c3p0结合spring提供的数据源路由(DataSource Routing)对象,该对象构造了一个存储多数据源的Map,可以根据指定的key动态的查找对应的数据源。

具体实现如下:

1.编写c3p0配置:

[html] view plain copy
  1. <!-- c3p0连接池配置 -->  
  2. <bean id="parentDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  3.     <property name="driverClass">  
  4.         <value>${jdbc.driverClassName}</value><!-- jdbc连接相关信息,单独编写一个properties文件 -->  
  5.     </property>  
  6.       
  7.     <!-- 一次性获取连接数,Default:3  -->  
  8.     <property name="acquireIncrement" value="5"/>  
  9.       
  10.     <!-- 初始化连接数,Default:3 -->  
  11.     <property name="initialPoolSize" value="3"/>  
  12.       
  13.     <property name="minPoolSize" value="1"/>  
  14.     <!-- 连接池中保留的最大连接数,Default:15 -->  
  15.     <property name="maxPoolSize" value="20"/>  
  16.       
  17.     <!-- 获取连接失败后,重复尝试的次数,Default:30 -->  
  18.     <property name="acquireRetryAttempts" value="30"/>  
  19.       
  20.     <!-- 获取连接超时时间,超时后将抛出SQLException,Default:0,单位:毫秒 -->  
  21.     <property name="checkoutTimeout" value="18000"/>  
  22.       
  23.     <!-- 多长时间检查所有连接池中的空闲连接,Default:0,单位:秒 -->  
  24.     <property name="idleConnectionTestPeriod" value="1200"/>  
  25.       
  26.     <!-- 最大空闲时间内连接未使用,则被丢弃,Default:0,单位:秒 -->  
  27.     <property name="maxIdleTime" value="1800"/>  
  28.       
  29.     <!-- 取得连接的同时是否校验连接的有效性,Default:false -->  
  30.     <property name="testConnectionOnCheckin" value="true"/>  
  31. </bean>  

2.数据源配置:

[html] view plain copy
  1. <!-- dataSource配置 -->  
  2. <bean id="fromDataSource" parent="parentDataSource">  
  3.        <property name="jdbcUrl">  
  4.            <value>${jdbc.from.url}</value>  
  5.        </property>  
  6.        <property name="user">  
  7.            <value>${jdbc.from.username}</value>  
  8.        </property>  
  9.        <property name="password">  
  10.            <value>${jdbc.from.password}</value>  
  11.        </property>  
  12. </bean>  
  13. <bean id="toDataSource" parent="parentDataSource">  
  14.        <property name="jdbcUrl">  
  15.            <value>${jdbc.to.url}</value>  
  16.        </property>  
  17.        <property name="user">  
  18.            <value>${jdbc.to.username}</value>  
  19.        </property>  
  20.        <property name="password">  
  21.            <value>${jdbc.to.password}</value>  
  22.        </property>  
  23. </bean>  

3.动态数据源加载配置:

[html] view plain copy
  1. <!-- 动态DataSource配置 -->  
  2. <bean id="dynamicDataSource" class="com.shxt.power.util.DynamicDataSource">  
  3.     <property name="targetDataSources">  
  4.         <map key-type="java.lang.String">  
  5.             <entry key="fromDataSource" value-ref="fromDataSource"/>  
  6.             <entry key="toDataSource" value-ref="toDataSource"/>  
  7.         </map>  
  8.     </property>  
  9.     <property name="defaultTargetDataSource" ref="fromDataSource"/>  
  10. </bean>  

4.实现spring的数据源路由对象:

[java] view plain copy
  1. import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;  
  2.   
  3. public class DynamicDataSource extends AbstractRoutingDataSource {  
  4.   
  5.     @Override  
  6.     protected Object determineCurrentLookupKey() {  
  7.         String sourceType = CustomContextHolder.getCustomerType();  
  8.         //System.out.println("DataSourceType: "+sourceType);  
  9.         return sourceType;  
  10.     }  
  11. }  

5.编写自定义的数据源key:

[java] view plain copy
  1. public class CustomContextHolder {  
  2.     private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();  
  3.     public static final String DATA_SOURCE_FROM = "fromDataSource";//对应动态数据源配置中的key  
  4.     public static final String DATA_SOURCE_TO = "toDataSource";  
  5.   
  6.     public static void setCustomerType(String customerType) {  
  7.          contextHolder.set(customerType);  
  8.     }  
  9.   
  10.     public static String getCustomerType() {  
  11.         return contextHolder.get();  
  12.     }  
  13.   
  14.     public static void clearCustomerType() {  
  15.         contextHolder.remove();  
  16.     }  
  17. }  
原创粉丝点击