I struggled a lot for this months ago, so I'll try to sum up what worked for me (I admit such a use case should be described in the Spring reference documentation):
- use a
PersistenceUnitManager to manage the multiple PUs
- use multiple
LocalContainerEntityManagerFactoryBean beans to build your EMF and reference the PUM in it.
- specify the PU name for each EMF
- use JTA if you want a global transaction around your work on the 2 (or more) databases and use databases and drivers that support XA transactions in that case (I had some tricky problems with MySQL for example)
- inject the EM into your DAOs via the Spring configuration (@PersistenceContext(unitName="xxx") was commented in my code, but I don't remember if it should work, and I cannot test the full project for now)
You'll find below a configuration sample (I removed anything that is business specific) for 2 databases (one local, one remote) using:
- Spring 3.0 (I believe it's also usable with Spring 2.5.6),
- EclipseLink as a JPA provider (replace the specifics with Hibernate in your case),
- MySQL (or any database)
- Atomikos for a standalone JTA transaction manager (I didn't put the whole config, please refer to
http://www.atomikos.com/Documentation/SpringIntegration if needed, or use another JTA provider)
Hope this helps you, and maybe others to configure properly multiple databases access through JPA and Spring.