I have two entity managers in my applicationContext.xml
which corresponds to two different databases. I can easily query database1
with entit
I've hit the same exact issue, but with multiple Hibernate session factories: 2 DBs with the same structure, I didn't want to have 2 identical sets of DAOs, etc. While my experience was with Hibernate, I suspect you could use the same solution: Spring's AbstractRoutingDataSource
. It allows you to configure your app to determine at runtime which data source to use, based on a value set on the ThreadLocal
. See http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/ for an introduction. What ends up happening is that the dataSource
ref in your factory will point not at a hard-coded dataSource
bean, but at the AbstractRoutingDataSource
. To set the toggle per-thread, use an @Aspect to determine which DB to hit.