GeeCON Prague 2014*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Transaction scoped vs Extended persistance context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Transaction scoped vs Extended persistance context" Watch "Transaction scoped vs Extended persistance context" New topic
Author

Transaction scoped vs Extended persistance context

Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
Hi all,

so im going through the oreilly ejb3 book preparing for the exam. In the book it is said that if you call find,getReference,refresh or merge methods on an EntityManager instance that is transaction scoped, it will throw a TransactionRequired Exception. But the default value for @TransactionAttribute is always REUIRED yea? and i tried it out and calling those methods on a transaction scoped context didnt throw any exceptions. Why is it said so?
Any comments on this is greatly appreciated.

Regards,
Dinuka


if who i am is what i have, and what i have is lost, then who am i?<br /> <br />SCJP 5.0<br />SCWCD 1.4<br />SCBCD preparing
Tushar Roy
Greenhorn

Joined: Mar 26, 2008
Posts: 29
Hi Dinuka,

If you have container managed entity manager with transaction scope and if you call refresh, merge,remove or persist operation on entity manager outside the transaction context then it will throw TranscationRequiredException. For find operation the entity which will be returned will not be managed entity if you call from non transactional context. You are calling these methods in transaction scope so there wont b any error. Change the trnasaction attribute to NOT_SUPPORTED and then try....

Hope this helps....


Tushar<br /> <br />SCJP 5 (95%)<br />SCWCD 1.4 (92%)<br />SCBCD 5 (93%)
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
Hi Roy,

Thanks for taking the time to answer my question. So what you're saying is that basically if you call those methods in a method annotated with NOT_SUPPORTED, you will get a TransactionRequired Exception yea?

So does that mean if you call these methods within a method annotated by NOT_SUPPORTED within an extended persistence context that it will not throw this exception?

And basically you only need to use extended context only if you are dealing with stateful session beans yea?

And just one more thing. Say if you do call these methods in a NOT_SUPPORTED enviroment within a transaction scoped context and you say manager.joinTransaction and call those methods after that method call,then it should not throw those exceptione yea?

Thanks again for you reponse

Regards,
Dinuka
Tushar Roy
Greenhorn

Joined: Mar 26, 2008
Posts: 29
Yes if you call these methods in extended persisitence context outside transaction context(in stateful sessions only) it will not throw TransactionRequiredException.

If you call entityManager.joinTransaction outside transaction scope it will itself give transactionRequiredException. You need a transaction context to call this method aswel...
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
Hi Roy,

Thanks again for you quick response. I have just one last question. Could you tell me the use of entitymanager.joinTransaction method? Can you give me a scenario that it is used in? It is used to associate an instance of an EntityManager with an active JTA Transaction yea? But doesnt ejb's have transactions associated with them automatically? Why would we need to use this method ?

Regards,
Dinuka
Tushar Roy
Greenhorn

Joined: Mar 26, 2008
Posts: 29
hmmm......There are 2 types of entity manager........Container managed Entity manager and Application managed entity manager....Container managed EM automatically joins the transaction(so you dnt need to use joinTransaction method)...But application managed entity manager created outside the transaction context does not automatically joins the transaction....SO you need joinTransaction method to make them join the transaction....

eg.

EntityManager em = emf.createEntityManager();
UserTransaction ut;
ut.begin()
em.joinTransaction();
...
...
ut.commit();

Here entity manager is created outside the transaction context so joinTransaction() is used....
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
Great explanation. Thanks alot for taking the time to explain this concept to me Roy. It was a bit confusing at first. But now its all clear. Thanks again.
 
GeeCON Prague 2014
 
subject: Transaction scoped vs Extended persistance context