Multiple EntityManagerFactories for the same DB - do i need JTA

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Multiple EntityManagerFactories for the same DB - do i need JTA

mem12
Hello,

i've a somewhat odd question i can't find an answer for.
We've a modular application where each module which accesses the db has it's own entity manager factory and persistence unit. They all use the same DB and can be involved in a Transaction either alone or multiple at once. We're currently using Bitronix (through Spring) but are trying to evaluate the different vendors like Jboss, Atomikos, Bitronix, ... as we're unsure about the exact functionality.
Everything seems to work just fine but today i was debugging a transaction which had to different EntityManagers involved but then during the commit phase the Bitronix Commiter class in the commit method figured there's only 1 single resource involved and therefore there's no need for 2PC (which is the main purpose of JTA, right?).
So, to sum up, do i need JTA and 2PC when using multiple EntityManagers which all connect to the same DB?

Thanks in advance.
Matt
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Multiple EntityManagerFactories for the same DB - do i need JTA

Ludovic Orban-2
If you want the changes you make via the different persistence units to be part of the same transaction, you have to use a transaction manager.

Here's an example:

@Transactional
public void myBusinessMethod() {
  doSomeChangesUsingPersistenceUnit1();
  doSomeChangesUsingPersistenceUnit2();
}

If you want to guarantee that the changes made by doSomeChangesUsingPersistenceUnit1 are rolled back when doSomeChangesUsingPersistenceUnit2 fails, you have to use a transaction manager. That obviously being only one example of guarantee a TM would provide you with.

1PC is an optimization that automatically kicks in when only one resource (ie, in your case: a single persistence unit) is being part of a transaction. It is impossible for me to tell you exactly why you see it happening but a probable cause is that you're only using one one PU when you see this log which is quite normal. Other reasons would involve incorrect configuration of your datasources, transaction manager or incorrect transaction boundaries (ie: misplaced @Transactional tags or Spring transactional AOP interceptor).



2012/1/12 mem12 <[hidden email]>

Hello,

i've a somewhat odd question i can't find an answer for.
We've a modular application where each app which accesses the db has it's
own entity manager factory and persistence unit. They all use the same DB
and can be involved in a Transaction either alone or multiple at once. We're
currently using Bitronix (through Spring) but are trying to evaluate the
different vendors like Jboss, Atomikos, Bitronix, ... as we're unsure about
the exact functionality.
Everything seems to work just fine but today i was debugging a transaction
which had to different EntityManagers involved but then during the commit
phase the Bitronix Commiter class in the commit method figured there's only
1 single resource involved and therefore there's no need for 2PC (which is
the main purpose of JTA, right?).
So, to sum up, do i need JTA and 2PC when using multiple EntityManagers
which all connect to the same DB?

Thanks in advance.
Matt
--
View this message in context: http://old.nabble.com/Multiple-EntityManagerFactories-for-the-same-DB---do-i-need-JTA-tp33126315p33126315.html
Sent from the Bitronix Transaction Manager mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Multiple EntityManagerFactories for the same DB - do i need JTA

mem12
Hello,

thanks for your fast response.
So to confirm, you're saying if i'm using more than one JPA Persistence Unit in one JTA Transaction it has to be a 2PC no matter if they both point to the same Database/Datasource or not?
As i'm certain about using 2 PU's in my sample the result is that there's a misconfiguration with the DB or Spring config.

kind regards
Matt


Ludovic Orban-2 wrote
If you want the changes you make via the different persistence units to be
part of the same transaction, you have to use a transaction manager.

Here's an example:

@Transactional
public void myBusinessMethod() {
  doSomeChangesUsingPersistenceUnit1();
  doSomeChangesUsingPersistenceUnit2();
}

If you want to guarantee that the changes made by
doSomeChangesUsingPersistenceUnit1 are rolled back when
doSomeChangesUsingPersistenceUnit2 fails, you have to use a transaction
manager. That obviously being only one example of guarantee a TM would
provide you with.

1PC is an optimization that automatically kicks in when only one resource
(ie, in your case: a single persistence unit) is being part of a
transaction. It is impossible for me to tell you exactly why you see it
happening but a probable cause is that you're only using one one PU when
you see this log which is quite normal. Other reasons would involve
incorrect configuration of your datasources, transaction manager or
incorrect transaction boundaries (ie: misplaced @Transactional tags or
Spring transactional AOP interceptor).



2012/1/12 mem12 <memcacher@gmail.com>

>
> Hello,
>
> i've a somewhat odd question i can't find an answer for.
> We've a modular application where each app which accesses the db has it's
> own entity manager factory and persistence unit. They all use the same DB
> and can be involved in a Transaction either alone or multiple at once.
> We're
> currently using Bitronix (through Spring) but are trying to evaluate the
> different vendors like Jboss, Atomikos, Bitronix, ... as we're unsure about
> the exact functionality.
> Everything seems to work just fine but today i was debugging a transaction
> which had to different EntityManagers involved but then during the commit
> phase the Bitronix Commiter class in the commit method figured there's only
> 1 single resource involved and therefore there's no need for 2PC (which is
> the main purpose of JTA, right?).
> So, to sum up, do i need JTA and 2PC when using multiple EntityManagers
> which all connect to the same DB?
>
> Thanks in advance.
> Matt
> --
> View this message in context:
> http://old.nabble.com/Multiple-EntityManagerFactories-for-the-same-DB---do-i-need-JTA-tp33126315p33126315.html
> Sent from the Bitronix Transaction Manager mailing list archive at
> Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Multiple EntityManagerFactories for the same DB - do i need JTA

Ludovic Orban-2
If you share the exact same datasource between all your PUs, the datasource and/or Spring and/or Hibernate can do some magic to avoid the 2PC.

Once again, it's hard to tell if you misconfigured something or if some optimization managed to avoid the 2PC cycle. You may want to try to configure a different datasource per PU pointing to the same DB for validation purpose to make sure you do see 2PC happening like it should in that case.


2012/1/12 mem12 <[hidden email]>

Hello,

thanks for your fast response.
So to confirm, you're saying if i'm using more than one JPA Persistence Unit
in one JTA Transaction it has to be a 2PC no matter if they both point to
the same Database/Datasource or not?
As i'm certain about using 2 PU's in my sample the result is that there's a
misconfiguration with the DB or Spring config.

kind regards
Matt


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Multiple EntityManagerFactories for the same DB - do i need JTA

mem12
Thanks, you've been right, it works like expected when using two Datasources (even when they're pointing to the same DB), so it seems like the optimization magic avoids 2PC and works correctly.

Thanks for your help, probably would've spent hours or days debugging.

This means that using multiple JPA EntityManagerFactories/PersistenceUnits/EntityManagers in a modular application where they could be participating (together) in a Transaction you've to use JTA and it causes a 2PC. If the same DataSource is used some optimization logic saves the trouble of 2PC which is correct in this case.

One more question:
I've found an article:
http://www.javaworld.com/javaworld/jw-04-2007/jw-04-xa.html?page=5
about this topic which states:
"All the JTA implementations we looked at provide recovery test cases, which make it easy to run against the implementation itself, and on the participating XA resources as well."
We'd like to extensively test all our scenarios too but couldn't find any of the test cases so i'd appreciate if you could point me to where i can find them.

Once again, thanks for your help
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Multiple EntityManagerFactories for the same DB - do i need JTA

Ludovic Orban-2
Sorry but I have no idea what this article refers to. I'm not aware of any "recovery test cases which
make it easy to run against the implementation itself, and on the participating XA resources as well".

2012/1/12 mem12 <[hidden email]>

Thanks, you've been right, it works like expected when using two Datasources
(even when they're pointing to the same DB), so it seems like the
optimization magic avoids 2PC and works correctly.

Thanks for your help, probably would've spent hours or days debugging.

This means that using multiple JPA
EntityManagerFactories/PersistenceUnits/EntityManagers in a modular
application where they could be participating (together) in a Transaction
you've to use JTA and it causes a 2PC. If the same DataSource is used some
optimization logic saves the trouble of 2PC which is correct in this case.

One more question:
I've found an article:
http://www.javaworld.com/javaworld/jw-04-2007/jw-04-xa.html?page=5
about this topic which states:
"All the JTA implementations we looked at provide recovery test cases, which
make it easy to run against the implementation itself, and on the
participating XA resources as well."

We'd like to extensively test all our scenarios too but couldn't find any of
the test cases so i'd appreciate if you could point me to where i can find
them.

Once again, thanks for your help
--
View this message in context: http://old.nabble.com/Multiple-EntityManagerFactories-for-the-same-DB---do-i-need-JTA-tp33126315p33127102.html
Sent from the Bitronix Transaction Manager mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Multiple EntityManagerFactories for the same DB - do i need JTA

mem12
Hm, I was afraid so.
Sorry for bugging you so much but while debugging more i saw in the DiskJournal in the log method that only COMMITING and COMMITED and UNKNOWN need to be logged and the comment is that all others only make it easier to debug, i don't really get it how this makes it anyhow easier, where could you use that information?
Not sure if i understand it right, the only situation the Recoverer can recover something is if a transaction failed between commiting and commited, right? So, in case one or both resources succeed and then the app server crashes, who's recovering those prepared resources. I've seen in my DB in the table dba_pending_transactions that there's an entry for my transaction (shutdown app after 2PC prepare was successfully finished), after i startup my app again the Recoverer runs but doesn't find any dangling transactions as none was in state COMMITING or COMMITED. Still the entry in the dba_pending_transactions dissapears, is this any logic of the DB or is the JTA/Bitronix handling this?
Loading...