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 of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.