Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA UserTransaction Injection...  RSS feed

 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ...!!

Can anyone please tell me that when to use UserTransaction and when not to use it. Because i am getting errors sometimes when i have not used UserTransactions (in case of adding record to database through JPA in JSF managed bean) and sometimes when i don't even put UserTransaction in my code then also it works perfectly.( in case of Remote EJB bean inserting record into database)..

Therefore i am little confused regarding this. Can anyone please tell me when to use it and when to not... I read EJB 3.0 and JPA specification but still i am not clear with it..

Thanks in advance....
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPA can be configured to be JTA or RESOURCE_LOCAL (in your persistence.xml). When using JTA you must use JTA UserTransaction, with RESOURCE_LOCAL you can use JPA transaction.

A EJB SessionBean (or EJB MessageDrivenBean) will automatically begin and commit a JTA (UserTransaction) around any method call, so you don't need to call UserTransaction directly. Anything else, (JSF, JSP, etc.) will need to manage its own JTA transaction.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Transactions
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!