Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic