liveBook · Manning

Lost update anomaly

As explained in Java Persistence with Spring Data and Hibernate epub

A lost update occurs when one concurrent transaction overwrites another transaction's committed modification without detecting the conflict.

A lost update occurs when two concurrent transactions read the same data and then write their own changes without detecting a conflict. The later write overwrites the earlier update, so only one change survives. section 11.2.1

Two transactions change a price

Suppose two transactions read an item's buyNowPrice as 100. The first changes it to 120 and commits. The second, still working from the original value, writes 110 and commits. The first transaction's update is lost. section 11.2.1

Lost updates are an isolation problem. Other anomalies have different symptoms: a dirty read sees uncommitted changes, an unrepeatable read sees different values on successive reads, and a phantom read sees a changed query result. Stronger isolation can prevent anomalies, but complete isolation has a performance and scalability cost. section 11.2.1

Java Persistence automatic versioning provides another way to prevent lost updates. Mark an entity field with @Version; Hibernate loads the version and includes the old version in the update condition. If another transaction has changed the record, the stale update fails rather than silently overwriting it. In the example, Hibernate raises an OptimisticLockException at flush. section 11.2.2

Listing 11.2 Hibernate incrementing and checking the version automatically
Path: /Ch11/transactions/src/test/java/com/manning/javapersistence/ch11
 /concurrency/Versioning.java – firstCommitWins()
 
   EntityManager em1 = emf.createEntityManager();
   em1.getTransaction().begin();
Item item = em1.find(Item.class, ITEM_ID);                             #1
// select * from ITEM where ID = ?
assertEquals(0, item.getVersion());                                    #2
item.setName("New Name");
// . . .  Another transaction changes the record
assertThrows(OptimisticLockException.class, () -> em1.flush());        #3
// update ITEM set NAME = ?, VERSION = 1 where ID = ? and VERSION = 0
#1 Retrieving an entity instance by identifier loads the current version from the database with a <kbd>SELECT</kbd>.
#2 The current version of the <kbd>Item</kbd> instance is <kbd>0</kbd>.
#3 When the persistence context is flushed, Hibernate detects the dirty <kbd>Item</kbd> instance and increments its version to <kbd>1</kbd>. SQL <kbd>UPDATE</kbd> now performs the version check, storing the new version in the database, but only if the database version is still <kbd>0</kbd>.

Follow the loaded version, conditional SQL update, and exception for a conflicting Item modification. section 11.2.2

Checking only properties changed by the current transaction is narrower: a concurrent change to another property may go undetected. This strategy also does not work when merging a detached entity whose old values are unknown; the detached instance needs a version number or timestamp for optimistic concurrency control. section 11.2.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Traces competing reads and writes, then shows version checking at flush and limitations of dirty-property checks.
Examples
Uses competing buyNowPrice changes and a versioned Item update that throws an OptimisticLockException.
What you can do after
Apply optimistic concurrency control through Java Persistence versioning to detect conflicting updates.
How it connects
Connects transaction isolation with optimistic concurrency control, versioning, dirty reads, unrepeatable reads, and phantom reads.

Also explained in

Compare how 3 books explain lost update anomaly →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.