• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction scoped vs Extended persistance context

 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tushar Roy
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic