File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes JPA Spring: Connect to 2 databases Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "JPA Spring: Connect to 2 databases" Watch "JPA Spring: Connect to 2 databases" New topic

JPA Spring: Connect to 2 databases

Pablo Mino

Joined: Aug 11, 2009
Posts: 28
Hi, this is a subject that has posts here and there, but I am really not familiar with spring or JPA at all, so I need a little more detailed explanation

I had a web application with 1 persistence unit on persistence.xml, 1 dataSource 1 entityManagerFactory and 1 transactionManager on context.xml and 1 DAO with a @PersistenceContext

So now I need to make it 2 databases, not just one, thought 2 options:

1.- 2 persistence units 1 dataSource and 1 entityManagerFactory per each one, however JpaTransactionManager works for just 1 entityManagerFactory, found that I should use Jta instead of Jpa but no idea of how to do it

2.- Use 2 persistence units and 2 DAO, Spring suggest on its documentacion, JPA section, to create 1 persistenceUnitManager, point to the persistence.xml and nothing else

My context.xml now got 2 datasources and 1 persistenceUnitManager, but when I try to run it says no persistence unit found, I assume it is because JDBC information should be on each persistence unit and not on a bean in context.xml, is it? How I do that? And I really can't tell how my app tells which Datasource use when

Theres a 3rd option but I really didn't like it, is making a duplicate of everything and adding names even to @Transactional annotations... I don't want 2 applications running like one....

Please I would appreciate any help

PS: should I post this on ORM category too?
Pablo Mino

Joined: Aug 11, 2009
Posts: 28
Well I've been trying both things with some colleagues too

Looks like using a persistenceUnitManager works for 2 persistence Units from a same schema, am I right?, because we made it work but just only works with the default unit, if we try querying the 2nd unit it just crash, besides we can't tell if theres a mechanism to "switch" between the units.

Now, we are trying the JTA option, it does connect so the Datasources are fine but seems it fails when searching for a proper look-up class, can someone help me work the JTA transaction config from zero?

I really don't want to create 2 JPA transactions and specify on every single @Transaction which to use
Pablo Mino

Joined: Aug 11, 2009
Posts: 28
Ok I couldn't do anything, so I decided to go for option 3, and I got a new problem. Got the 2 persistence units, but each one binds all the entitys, when what i actually wanted is some to go with one persistence unit and some go with the other, I noticed I have no mechanism for that either, what can I do?

And this is my code


I agree. Here's the link:
subject: JPA Spring: Connect to 2 databases
It's not a secret anymore!