For one of my projects I needed to be able to have 1 codebase (1 web application) which connected to 40 different database. We needed something to switch the database connection (and in our case Hibernate SessionFactory) at runtime and per user request. For this we created something we called the ContextSwappableTargetSource. It consists of 3 classes 1 ContextHolder which is a ThreadLocal holder for a property (we set it with a filter) and the real ContextSwappableTargetSource class and an Exception class.
I noticed in the Spring forums that more people need something similair that is why I post my bit of code here. If it is interesting enough to use in the Spring codebase feel free. If not well it is documented for future reference
.
Added jar with classes and sources.
Note: I used SLF4J and Logback as the logging api.