• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA Spring: Connect to 2 databases

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

persistence.xml


appContext.xml
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic