Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » OptimisticLockException Data from Table not getting Refreshed(Subsequent query still refers to the old version of the record throwing Optimistic Lock Exception)
OptimisticLockException Data from Table not getting Refreshed [message #1861759] Thu, 02 November 2023 10:52 Go to next message
Amaladhithyan Krishnamoorthy is currently offline Amaladhithyan KrishnamoorthyFriend
Messages: 1
Registered: November 2023
Junior Member
Consider the below scenario:

Application uses eclipselink(JPA) to manage the updates in Database(oracle). Scope of the application is to take an ID from the incoming request and fetch the entire record from the DB for that ID. After few validations, update the same record with new values and commit the transaction. Each of the record in the DB has a REC version. i.e for each update to the record, the REC version is automatically incremented, based on which the number of updates to a record can be found out.

However during the update OptimisticLockException is thrown.

To explain further when these two web service requests are fired, below happens:

• Client A web_service_request_1- > Reads entity from DB with REC_VERSION value as 0- when it tries to update the related record - > First it compares that the REC_VERSION field has not changed to detect locking conflicts. If not, DB update will happen successfully. This will also update the REC_VERSION field for e.g. it will update REC_VERSION value 0 to 1 -> Successful.

• Client A web_service_request_2- > Reads entity from DB with REC_VERSION value as 0-when it tries to update the same record (updated last by web service request 1), first it compares that the REC_VERSION field has not changed to detect locking conflicts. -> But the REC_VERSION version field was already updated in the DB (from 0 to 1) by the web service request 1 . so in this case, conflict happens and this OptimisticLockException was thrown.

The reason for web_service_request_2 still getting old REC_VERSION value as 0 is unknown. Since the web_service_request_1 updated the REC_VERSION from 0-1, the web_service_request_2 should have got the latest REC_VERSION as 1 as updated by the web_service_request_1. Since this didn't happen OptimisticLockExpcetion is being thrown.

From Oracle Logs we could verify that web_service_request_1's commit was successful there by updating the values of the record and the REC_VERSION itself. However the eclipselink JPA layer is not able to get the latest update from the DB.

If the failed request is sent again after 10 mins to the same application, it passes and updates the DB without any issues.

Log:
Oct 05, 2023 5:27:11 PM org.eclipse.persistence.default.sql
FINE: UPDATE CONTRACT_ALL SET CO_MODDATE = ?, REC_VERSION = ? WHERE ((CO_ID = ?) AND (REC_VERSION = ?))
bind => [2023-10-05 17:27:11.498, 4, 2721830, 3]
Oct 05, 2023 5:27:11 PM org.eclipse.persistence.default
WARNING:
Local Exception Stack:
Exception [EclipseLink-5006] (Eclipse Persistence Services - 2.1.3.v20110304-r9073): org.eclipse.persistence.exceptions.OptimisticLockException
Exception Description: The object [com.lhs.Contract.bscs_core.Contract@23855c6] cannot be updated because it has changed or been deleted since it was last read.
Class> com.lhs.Contract.bscs_core.Contract Primary Key> 2,721,830
at org.eclipse.persistence.exceptions.OptimisticLockException.objectChangedSinceLastReadWhenUpdating(OptimisticLockException.java:137)


We understand that OptimisticLockException is thrown to prevent "update at all cost" and there by maintaining data integrity when parallel updates happen in the table.

However in this scenario no parallel updates are being made to the table only one request at a time is being fired.

As we checked there was no network loss/interruption between JPA and the DB. (Suspected network problem because updates went if there is a 10 min gap between subsequent request)

Is there any setting that we need to set a eclipselink level to refresh the object from the DB? Or any configuration to handle it?

Re: OptimisticLockException Data from Table not getting Refreshed [message #1861864 is a reply to message #1861759] Wed, 08 November 2023 15:37 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 54
Registered: December 2021
Member
Turn on EclipseLink logging and you should be able to see the query for update statement to change the rec value from 0->1 as well as the fetch of the entity for the second request. Timing could be an issue if the commit hasn't completed before the second request performs the fetch, but how you are performing the second fetch is more likely the problem. Make sure that you are not using an existing EntityManager instance that may already have loaded this entity with the stale 0 rec value. EntityManager instances maintain a cache of everything read in through them, and I've seen applications trying to manage and reuse those instances for reads and hit this issue. You haven't described anything about your contexts though, so all we can do is guess.

Best Regards,
Chris
Previous Topic:distinct with order by sql query not working in eclipsleink
Next Topic:MappedSuperClass with EclipseLink
Goto Forum:
  


Current Time: Sun Apr 28 02:18:09 GMT 2024

Powered by FUDForum. Page generated in 0.03420 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top