aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes JTA Transaction on Stateful Session Bean. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "JTA Transaction on Stateful Session Bean." Watch "JTA Transaction on Stateful Session Bean." New topic
Author

JTA Transaction on Stateful Session Bean.

Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
From WhizLabs, Diagnostic Exam, Question 39

WhizLabs wrote:If a JTA transaction is propagated into the stateful session bean to which an extended persistence context is bound but with the JTA bound persistence context is different, container throws an EJBException
.

Is this correct? I actually couldn't understand this.
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
It is correct. Here's why: suppose the SFSB has an extended persistent context, and then another transaction is started elsewhere, and a persistent context is bound to it, and eventually the call is made to the SFSB. Now, if the transaction is propagated to the SFSB (it might not be, if the SFSB method is TransactionAttributeType.NOT_SUPPORTED for example), there are 2 persistence contexts available - the original, extended context, and the new one, propagated with the transaction. There is a clash. Which one should the persistence provider pick and use? It is a hard decision, that hard to throw an exception, actually ;-)
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Raf,

Sorry but I couldn't understand this. How it is possible to have two persistence context available at the same time? Shouldn't there be a single persistence context for every transactions?
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
Yes, there should be only one, but you have only one context for a transaction, and the second is the extended context that the SFSB had before, and which is bound to the lifecycle of the bean. So, there would effectivaly be 2 contexts.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
This is what I did to understand this.







The client calls to the firstMethod() of the FirstServiceBean. But I didn't get an exception here.


Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
That's probably because you are not invoking the entity manager in the first bean - the context is simply not created, as JTA contexts are created when first call to an EntityManager is made.

Specs 5.6.1:

A new persistence context begins when the container-managed entity manager is invoked in the
scope of an active JTA transaction, and there is no current persistence context already associated with
the JTA transaction. The persistence context is created and then associated with the JTA transaction.


So, this would imply that you are propagating the transaction, but as no persistent context was created, it is not propagated.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Oh Thanks Raf, I managed to get the EJBException.
Hay, how do you memorized all of the concepts in EJB specs and JPA specs?
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
I studied ;-) And wrote code, and studied some more.
And I do make mistakes, you know that already.
 
Don't get me started about those stupid light bulbs.
 
subject: JTA Transaction on Stateful Session Bean.
 
Similar Threads
Propogation
Application managed EntityManagers with transactions.
Persistence Context Propagation
SLSB Transaction scoped EntityManager calling SFSB Extended EntityManager
Transaction Scoped Persistence Contexts