Tuesday, December 21, 2004

Datasources selection with Spring

The aim is to permit to an application (and the user) to select a specific datasource to use, so a user can choose a specific database and datas to access. We can use the same mechanism than Acegi: a filter to get the datasource name and place it in known holder ( based on a thread local ) and a specific datasource ( proxy on datasources ) to get this name in order to select the datasource to use.

- Datasource name context: it's in fact a class to store the name of the DataSource et a class (a simple ThreadLocal) to hold it (the known place).
Here is an example of these two classes and interfaces:

public class DataSourceContextHolder {
private static ThreadLocal contextHolder = new ThreadLocal();

public static void setContext(DataSourceContext context) {

public static DataSourceContext getContext() {
return (DataSourceContext) contextHolder.get();

public interface DataSourceContext {
public void setId(String id);
public String getId();

public class DataSourceContextImpl implements DataSourceContext {
private String id;

public void setId(String id) {

public String getId() {
return id;

- Filter to detect context: the filter look for an datasource id in the session and put it in the context holder. The filter might be enhanced to have the feature to select the id and block this change when a user is authenticated on a database.

- Datasource selector: thanks to Spring, there is a datasource abstraction so we can put a proxy between our datasource and application. Our datasource proxy could select another datasource configure in Spring at runtime using the datasource context.
Here is an example of this proxy:

public class DataSourceSelector extends AbstractDataSource
implements BeanFactoryAware {
private BeanFactory beanFactory;

public void setBeanFactory(BeanFactory beanFactory)
throws BeansException {

public Connection getConnection() throws SQLException {
DataSourceContext context=DataSourceContextHolder.getContext();
DataSource datasource=(DataSource)beanFactory.getBean(
return datasource.getConnection();

public Connection getConnection(String login,String password)
throws SQLException {
DataSourceContext context=DataSourceContextHolder.getContext();
DataSource datasource=(DataSource)beanFactory.getBean(
return datasource.getConnection(arg0,arg1);

This mechanism is independant of the application (Spring offers this
facility!) but we must be carefull to not permit to access a database
without be authenticated. The order of filter (with use of security filter such Acegi's) could be dangerous... The solution is not to permit to switch from
a datasource to another when a user is authenticated.

1 comment:

Anonymous said...

There are wow gold 3 men in a plane. wow gold A priest, a baseball player world of warcraft power leveling and a soldier. the pilot goes into dog clothes the cabin and 2moons gold tells them that there Atlantica gold is a weight overload 2moons power leveling on the plane and they would each have to throw world of warcraft gold one personalworld of warcraft power leveling belonging out aoc power leveling the window to safely land. Power Leveling the priest throws flyff money out a bible and says,"forgive me lord, i will last chaos gold get another one.archlord power leveling The baseball player world of warcraft gold throws a baseball out and says, aoc gold "i play professional baseball, i can get thesearchlord gold whenever i want." Now