Query is not fetching the latest data from MySQL while using FOR UPDATE clauses

I have a table which locks rows as per accountId and a lock status column having values 0 and 1.

CREATE TABLE lock (
   id INT(11),
   accountId INT(11),
   isLocked TINYINT(2),
);

The requirement is the that multiple threads could try to take the lock. However, only one of them will be successful. I am implementing lock by using a pessimistic write lock which fires a “SELECT … FOR UPDATE” query.

The code flow is like below

Controller -> Service -> DAO -> DB

DAO contains @Transactional annotation so that read and write will happen in a single transaction.

The DAO code is like below

@Override
@Transactional
public boolean getLock(Account account) {
    boolean isLockAcquired = false;
    QLock qLock = QLock.lock;

    JPAQuery<Lock> lockJpaQuery = new JPAQuery<>(entityManager);
         
    lockJpaQuery.setLockMode(LockModeType.PESSIMISTIC_WRITE).from(qLock)
                .where(qLock.account.id.eq(account.getId()));

    Lock lock = lockJpaQuery.fetchOne();

    if (lock.getIsLocked() == 0) {
        lock.setIsLocked(1);
        isLockAcquired = true;
    }
    lockRespository.save(lock);
    return isLockAcquired;
}

Once the thread completes the work, it uses below code to release the lock.

@Override
@Transactional
public boolean releaseLock(Account account) {
        
    boolean isLockReleased = false;

    JPAQuery<Lock> lockJpaQuery = new JPAQuery<>(entityManager);
    QLock qLock = QLock.lock;

    lockJpaQuery.setLockMode(LockModeType.PESSIMISTIC_WRITE).from(qLock)
                .where(qLock.account.id.eq(account.getId()));

    Lock lock = lockJpaQuery.fetchOne();

    if(lock.getIsLocked == 1) {
        lock.setIsLocked(0);
        lockRespository.save(lock);
        isLockReleased = true;
    }
    return isLockReleased;
}

An example call for this method from service is below.

boolean lockStatus = dao.getLock(account);

while(!lockStatus) {
    lockStatus = dao.getLock(account);
}

.... Do some work

lockStatus = dao.releaseLock(account);

Now to test this code, I started server in debug mode and acquired lock by the first request and set isLocked to 1. The code would have acquired the lock, changed the isLocked field and releases the pessimistic write lock.
As transaction is only upto the getLock method, pessimistic write lock should be removed and now other threads can take the lock on the row. I can also see isLocked value changed from 0 to 1.

Now, I fired one more request to see if it will get the lock or not. As expected it didn’t as even if it can hold pessimistic write lock on the row, it can’t modify it as its value is already 1. So, it went into the while loop.

So, I switched the first thread and complete its process and again acquired the lock and set the value of isLocked to 0 and then it exited. The value of isLocked also changed in the database.

Post this when I proceeded with the execution of second thread, the getLock method still returns false. When I debugged in the method itself, it is returning a Lock object which is having isLocked=1 due to which it is not able to modify it as per condition and unable to get the lock.

My issue here is even when the first thread has updated the value why second thread still fetching the old value?

Steps I followed to check:

  1. I printed the query fired by second thread and ran it on db client. It worked fine.
  2. I changed my isolation level from REPEATABLE-READ to READ-COMMITTED.
  3. I checked whether a transaction is being created or not using TransactionAspectSupport.

Important Points:

  1. I am using MySQL 8.0.
  2. Isolation level is READ-COMMITTED.
  3. Service class method don’t have @Transactional annotation.
  4. The value changes from the first thread are visible in database.
  5. I tried resetting the entityManager cache, but it didn’t work.
  6. I am using QueryDSL.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật