论坛首页 Java企业应用论坛

SSH配置动态数据源

浏览 7097 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-04-17  

用到一个项目,需要整合2个不同的数据库!

现将代码贴下,以备后用:

1、创建静态映射类,该类映射动态数据源

public class DataSourceMap {
	 public static final String Analyse="Analyse";    
	 public static final String DLmarket= "DLmarket";  
}

 

2、创建数据库连接配置容器 类

public class DataSourceContextHolder {
	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();
	}

}

 

3、创建动态数据源切换类

public class DynamicDataSource extends AbstractRoutingDataSource{

	@Override
	protected Object determineCurrentLookupKey() {
		// TODO Auto-generated method stub
		String customerType="";
		if(DataSourceContextHolder.getCustomerType()!=null){
			customerType = DataSourceContextHolder.getCustomerType().toString();
		}
		return customerType;
	}
	
}

 该类继承AbstractRoutingDataSource,并重写determineCurrentLookupKey方法

 

4、在spring中配置多数据源

 

<bean id="analyseDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
xxx...
</bean>

<bean id="dlmarketDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url_dlmarket}" />
<property name="username" value="${jdbc_username_dlmarket}" />
<property name="password" value="${jdbc_password_dlmarket}" />
xxx...
<bean>

<!-- 多数据源的映射关系 -->
<bean id="dataSource" class="com.current.util.DynamicDataSource">
	<property name="targetDataSources">
	<map key-type="java.lang.String">
	<!-- key的值必须要和静态键值对照类中的值相同&nbsp; -->
		<entry value-ref="analyseDataSource" key="Analyse"></entry>
		<entry value-ref="dlmarketDataSource" key="DLmarket"></entry>
	</map>
	</property>
	<property name="defaultTargetDataSource" ref="analyseDataSource"></property>
</bean>

 

其他的SessionFactory   事务管理器配置都不需要修改。

 

5、在Action中切换数据源

public void getList(){
		DataSourceContextHolder.setCustomerType(DataSourceMap.DLmarket);
		int id=1;
		List<Chaining> chainList = chainService.getList(id);
		System.out.println(chainList.get(0).getChaining());	
	}

 

 

   发表时间:2013-04-18   最后修改:2013-04-18
protected Object determineCurrentLookupKey() {}
这个方法里的判断有点多此一举了,DataSourceContextHolder.getCustomerType()这句代码的执行是紧接着 DataSourceContextHolder.setCustomerType(DataSourceMap.DLmarket)的,都是在同一个线程里,放心去掉 if 吧
0 请登录后投票
   发表时间:2013-04-18  
luoyu-ds 写道
protected Object determineCurrentLookupKey() {}
这个方法里的判断有点多此一举了,DataSourceContextHolder.getCustomerType()这句代码的执行是紧接着 DataSourceContextHolder.setCustomerType(DataSourceMap.DLmarket)的,都是在同一个线程里,放心去掉 if 吧


呵呵,是的!
一个action里面有很多方法,只要切换一次对么 ?
不需要每个方法都切换对吧?
0 请登录后投票
   发表时间:2013-04-19  
单一色调 写道
luoyu-ds 写道
protected Object determineCurrentLookupKey() {}
这个方法里的判断有点多此一举了,DataSourceContextHolder.getCustomerType()这句代码的执行是紧接着 DataSourceContextHolder.setCustomerType(DataSourceMap.DLmarket)的,都是在同一个线程里,放心去掉 if 吧


呵呵,是的!
一个action里面有很多方法,只要切换一次对么 ?
不需要每个方法都切换对吧?

只要你调用DataSourceContextHolder.setCustomerType(DataSourceMap.DLmarket)后,直到你下次再次调用该方法连接才会改变,这就要看你同一个action里的方法是不是大多数都用一个数据源
1 请登录后投票
   发表时间:2013-04-20  
看的不是很懂
0 请登录后投票
   发表时间:2013-04-24  
单一色调 写道

用到一个项目,需要整合2个不同的数据库!

现将代码贴下,以备后用:

1、创建静态映射类,该类映射动态数据源

public class DataSourceMap {
	 public static final String Analyse="Analyse";    
	 public static final String DLmarket= "DLmarket";  
}

 

2、创建数据库连接配置容器 类

public class DataSourceContextHolder {
	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();
	}

}

 

3、创建动态数据源切换类

public class DynamicDataSource extends AbstractRoutingDataSource{

	@Override
	protected Object determineCurrentLookupKey() {
		// TODO Auto-generated method stub
		String customerType="";
		if(DataSourceContextHolder.getCustomerType()!=null){
			customerType = DataSourceContextHolder.getCustomerType().toString();
		}
		return customerType;
	}
	
}

 该类继承AbstractRoutingDataSource,并重写determineCurrentLookupKey方法

 

4、在spring中配置多数据源

 

<bean id="analyseDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
xxx...
</bean>

<bean id="dlmarketDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url_dlmarket}" />
<property name="username" value="${jdbc_username_dlmarket}" />
<property name="password" value="${jdbc_password_dlmarket}" />
xxx...
<bean>

<!-- 多数据源的映射关系 -->
<bean id="dataSource" class="com.current.util.DynamicDataSource">
	<property name="targetDataSources">
	<map key-type="java.lang.String">
	<!-- key的值必须要和静态键值对照类中的值相同&nbsp; -->
		<entry value-ref="analyseDataSource" key="Analyse"></entry>
		<entry value-ref="dlmarketDataSource" key="DLmarket"></entry>
	</map>
	</property>
	<property name="defaultTargetDataSource" ref="analyseDataSource"></property>
</bean>

 

其他的SessionFactory   事务管理器配置都不需要修改。

 

5、在Action中切换数据源

public void getList(){
		DataSourceContextHolder.setCustomerType(DataSourceMap.DLmarket);
		int id=1;
		List<Chaining> chainList = chainService.getList(id);
		System.out.println(chainList.get(0).getChaining());	
	}

 

 

 

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics