Is there any Transaction Isolation problem with entityManager.find(class, id) ?

Solution Verified - Updated -

Issue

  • Concurrent transactions are not seeing updates made by other transactions.

  • How can we lock and fetch and entity so that changes to the entity are garanteed?

Environment

  • Enterprise Application Platform (EAP)
    • 4.2
    • 4.3
    • 5.0
  • Java Persistence API (JPA)
  • Flow is the following:

    T1 = transaction 1
    T2 = transaction 2
    T1 begin
    T1 price = em.find(Price.class, id); => value is 100
    T2 begin
    T2 price = em.find(Price.class, id); => value is 100
    T1 em.lock(price , LockModeType.WRITE);
    T2 em.lock(price , LockModeType.WRITE); => WAITS UNTIL T1 COMMITS
    T1 price.setValue(price.getValue() + 1); => new price is 101
    T1 commits
    T2 price.setValue(price.getValue() + 1); => new price is 101
    T2 commits
    => price = 101 => WRONG, should be 102
    

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content