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.
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
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....
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
Joined: Mar 26, 2008
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...
Joined: Aug 12, 2006
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 ?
Joined: Mar 26, 2008
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....