-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Multiples Persitence Unit in same Hibernate Session
PostPosted: Tue Sep 30, 2008 1:51 pm 
Newbie

Joined: Mon Aug 27, 2007 4:08 am
Posts: 8
Hi

i try to use 2 persistence unit in a same Hibernate Session. ..

it is possible?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2008 2:01 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
What is a "persistence unit"?

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 4:52 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
gonzao_diaz wrote:
What is a "persistence unit"?

eh, come on, gonzao, you know it!

In JPA, you define your persistent units in "persistence.xml" like this:
Code:
<persistence-unit name="dpjw">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/dpjwDS</jta-data-source>

      <mapping-file>META-INF/orm_dpjw.xml</mapping-file>

      <class>org.dpjw.base.domain.Check</class>
(...)
</persistence-unit>

<persistence-unit name="dpjw_office">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/dpjw_office</jta-data-source>

      <mapping-file>META-INF/orm_dpjw_office.xml</mapping-file>

      <class>org.dpjw.base.domain.Bank</class>
(...)
</persistence-unit>


In your entities, you describe per annotation, to which persistent units they belong; i.e:
Code:
@Entity
@Table(name="BANK", schema="dpjw_office")


So, as in this example, I have to 2 persistent units, referring to 2 different databases (or database schemas).

My experience with this situation was:

- The transaction manager of JBoss, where my JPA app is running on, threw an exception complaining that the datasources I am using, that is: the JDBC driver (here: MySQL), is not capable of taking part in a 2 phase commit scenario.

- I had to resort to some code trying to manual emulate a 2PC: which isn't save. This means: I annotated an EJB which handles the other datasource with
Code:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

Then I could invoke methods on that EJB from a transaction method of the EJB connected to the first datasource.

As I said: this is not save enough. If the inner transaction (created with REQUIRES_NEW) succeeds and commits, and the outer transaction fails, you would have to manually reset the results of the inner transaction; you cannot rollback it anymore.

So, a much better way is to split the application into separate processes with services operating on just one datasource, and then calling a service on the other application which has it's own transaction processing.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 11:57 am 
Newbie

Joined: Mon Aug 27, 2007 4:08 am
Posts: 8
Thanks Carlo,

Maybe the question wasn't clear. My doubt or problem is in the jar packaging, because my architecture supports modules, that comes in jars, but the entities of this jars are outside of the principal persistence unit

the only way to resolve es create entries with entities in principal persistence unit.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.