Multiple Entity Manager issue in Spring when using more than one datasource

前端 未结 1 1810
小蘑菇
小蘑菇 2020-12-08 12:48

I have two entity managers in my applicationContext.xml which corresponds to two different databases. I can easily query database1 with entit

相关标签:
1条回答
  • 2020-12-08 12:51

    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.

    0 讨论(0)
提交回复
热议问题