File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes difference between modifying a persistent entity within a transaction and outside a transaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "difference between modifying a persistent entity within a transaction and outside a transaction" Watch "difference between modifying a persistent entity within a transaction and outside a transaction" New topic
Author

difference between modifying a persistent entity within a transaction and outside a transaction

Ann Basso
Ranch Hand

Joined: Jul 26, 2007
Posts: 48
Hi,
The following code is from JPWH page, 423:


The following is a statement regarding this code given on the same page:

In the first transaction, you retrieve an Item object. The transaction then completes, but the item is still in persistent state. Hence, in the second transaction, you not only load a User object, but also update the modified persistent item when the second transaction is committed (in addition to an update for the dirty user instance).


My doubt is if Item is being updated upon the next commit anyway, what is the difference between modifying a persistent entity within a transaction and outside a transaction? In other words what is the difference between the following two codes -



and



Please help me understand this.

thank you!
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Performing the update inside the transaction context is more correct.

Depending on your JPA provider both may work, but the first one is unspecified in the JPA spec I think, so may not be portable.

Also some JPA operations such as persist(), merge(), remove() may require an active transaction.

If your EntityManager is JTA managed, then making changes inside the JTA transaction boundary is required.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between modifying a persistent entity within a transaction and outside a transaction