Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Joe Ess
  • salvin francis

Multiple Database

Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I need to switch databases at run time, in my application user is allowed to choose the database from the list of databases, I try to use AbstractRoutingDataSource

this is my java class which sets the new Database

package com.ncsts.view.bean;

import org.springframework.util.Assert;

public class ChangeContextHolder {
public enum DataBaseType {


private static final ThreadLocal<DataBaseType> contextHolder =
new ThreadLocal<DataBaseType>();
public static void setDataBaseType(DataBaseType dataBaseType) {
Assert.notNull(dataBaseType, "DataBaseType cannot be null");

public static DataBaseType getDataBaseType() {
return (DataBaseType)contextHolder.get();


public static void clearDataBase() {

this is my class which extends AbstractRoutingDataSource

package com.ncsts.view.bean;

import java.util.Map;

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

public class RoutingDataSource extends AbstractRoutingDataSource{
private Map targetDataSources;
private Object defaultTargetDataSource;

protected Object determineCurrentLookupKey() {
return ChangeContextHolder.getDataBaseType();


this is my context.xml file

<bean id="parentDataSource"
<property name="driverClassName" value="${db.driverClassName}"></property>
<property name="username" value="${db.1.username}"></property>
<property name="password" value="${db.1.password}"></property>
<bean id="db3DataSource" parent="parentDataSource">
<property name="url" value="${db.1.url}"></property>
<bean id="db2DataSource" parent="parentDataSource">
<property name="url" value="${db.2.url}"></property>

<bean id="dataSource"
<property name="targetDataSources">
<entry key="com.ncsts.view.bean.ChangeContextHolder.DataBaseType.DEMO1"
value-ref="db3DataSource" />
value-ref="db2DataSource" />
<property name="defaultTargetDataSource" ref="db2DataSource" />

can anybody help me to point out where I am going wrong
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your message it is not clear, what problem you are facing. Also formatting the code will increase the readability and people will be able to help you out.
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to accomplish with ThreadLocal? If this is for a web application, I don't think this class will behave the way you're expecting.
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!