aspose file tools*
The moose likes Other Application Frameworks and the fly likes Multiple Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Multiple Database" Watch "Multiple Database" New topic
Author

Multiple Database

Krishnakumar Ramanathan
Greenhorn

Joined: Aug 04, 2008
Posts: 6
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 {
DEMO1,
DEMO2,
DEMO3,

}

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

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

}

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

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;


@Override
protected Object determineCurrentLookupKey() {
return ChangeContextHolder.getDataBaseType();
}

}

this is my context.xml file

<bean id="parentDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
abstract="true">
<property name="driverClassName" value="${db.driverClassName}"></property>
<property name="username" value="${db.1.username}"></property>
<property name="password" value="${db.1.password}"></property>
</bean>
<bean id="db3DataSource" parent="parentDataSource">
<property name="url" value="${db.1.url}"></property>
</bean>
<bean id="db2DataSource" parent="parentDataSource">
<property name="url" value="${db.2.url}"></property>
</bean>

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


can anybody help me to point out where I am going wrong
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
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.


My Blog
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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.


A good workman is known by his tools.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Database
 
Similar Threads
Queue and session is closed! problem
Multi DataBase Connectivity with Spring and Jdbc
Access hibernate session inside onMessage using ActiveMQ
Spring JDBC Transaction
Getting request object outside of DispatchServlet.