Proxool配置多数据源动态切换

 

前段时间遇到多数据源动态切换问题,总结一下,做个记录,以备后续之需!

 

首先附上proxool连接池的配置方法:http://312350968.iteye.com/blog/1697508

 

 

proxool连接池是相对不叫稳定的第三方连接池,可以很好的跟spring结合,实现项目中动态切换数据源,大体的实现原理是继承AbstractRoutingDataSource类来实现动态切换.

具体代码如下:

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import com.mmc.util.LogUtil;
/**
 * @author Wang Huifeng
 *
 */
public class DynamicDataSource extends AbstractRoutingDataSource  {  
	/**
	 * 动态切换数据源
	 */
    @Override  
    protected Object determineCurrentLookupKey() { 
    	Object key=DataSourceContextHolder. getDataSourceType();
    	if(key==null){
    		LogUtil.log("当前数据源是:"+DataSourceType.DB1);
    	}else{
    		LogUtil.log("当前数据源是:"+key);
    	}
    	
    	return key;
    }

}

 

 

/**
 * @author Wang Huifeng
 *
 */
public class DataSourceType {
	public static final String DB1="db1";
	public static final String DB2="db2";
}
 

 

/**
 * @author Wang Huifeng
 *
 */
@SuppressWarnings("unchecked")
public class DataSourceContextHolder {
	private static final ThreadLocal contextHolder = new ThreadLocal();

	public static void clearDataSourceType() {
		contextHolder.remove();
	}

	public static String getDataSourceType() {
		return (String) contextHolder.get();
	}

	public static void setDataSourceType(String dataSourceType) {
			contextHolder.set(dataSourceType);
	}
}
下面是是项目中如何调用以及spring配置文件中如何配置多数据源:

proxool的配置文件,目前该配置文件只有两个数据源

## proxool configuration
jdbc-0.proxool.alias=db1
jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver
jdbc-0.proxool.driver-url=jdbc\:mysql\://192.168.1.221\:3306/demo3?useUnicode\=true&characterEncoding\=utf-8
jdbc-0.user=root
jdbc-0.password=123456
jdbc-0.proxool.minimum-connection-count=25
jdbc-0.proxool.maximum-connection-count=100
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
jdbc-0.proxool.statistics=1m,15m,1d
jdbc-0.proxool.house-keeping-sleep-time=6000
jdbc-0.proxool.simultaneous-build-throttle=20
jdbc-0.proxool.prototype-count=3
jdbc-0.proxool.trace=true

## proxool configuration
jdbc-1.proxool.alias=db2
jdbc-1.proxool.driver-class=com.mysql.jdbc.Driver
jdbc-1.proxool.driver-url=jdbc\:mysql\://192.168.1.221\:3306/demo2?useUnicode\=true&characterEncoding\=utf-8
jdbc-1.user=root
jdbc-1.password=123456
jdbc-1.proxool.minimum-connection-count=25
jdbc-1.proxool.maximum-connection-count=100
jdbc-1.proxool.house-keeping-test-sql=select CURRENT_DATE
jdbc-1.proxool.statistics=1m,15m,1d
jdbc-1.proxool.house-keeping-sleep-time=6000
jdbc-1.proxool.simultaneous-build-throttle=20
jdbc-1.proxool.prototype-count=3
jdbc-1.proxool.trace=true



该方法是如何切换数据源
DataSourceContextHolder.setDataSourceType(DataSourceType.DB1);

改段代码是如何在spring中配置多数据源:

<!-- Proxool 数据源 -->
	<bean id="dataSource1"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
		</property>
		<property name="url">
			<value>proxool.db1</value>
		</property>
	</bean>
	
	<bean id="dataSource2"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
		</property>
		<property name="url">
			<value>proxool.db2</value>
		</property>
	</bean>
	
	<bean id="dataSource" class="com.mmc.dataSourceUtil.DynamicDataSource">
		<property name="targetDataSources">
			<map key-type="java.lang.String">
				<entry key="db1" value-ref="dataSource1" />
				<entry key="db2" value-ref="dataSource2" />
			</map>
		</property>
		<property name="defaultTargetDataSource" ref="dataSource1" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
		<!-- 扫描带注解的实体类 -->
		<property name="packagesToScan">
			<list>
				<value>com.mmc.pojo</value>
			</list>
		</property>
		<!-- 提供注解扫描类的设置 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<!--
					<prop key="hibernate.show_sql">true</prop> <prop
					key="hibernate.format_sql">true</prop>
				-->
				<prop key="hibernate.jdbc.batch_size">50</prop>
				<prop key="hibernate.connection.autocommit">true</prop>
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
				<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
			</props>
		</property>

	</bean>

你可能感兴趣的:(Proxool动态切换数据源)