Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Unrecoverable OptimisticLockException(Entities in cache get out of sync)
Unrecoverable OptimisticLockException [message #1839854] Tue, 30 March 2021 09:27 Go to next message
Thomas Buckel is currently offline Thomas BuckelFriend
Messages: 2
Registered: March 2021
Junior Member
Hi all,

I'm maintaining a fairly extensive web application which uses Eclipselink 2.6.3 / Spring / Spring Data.

Occasionally we run into situations where one of our key entities gets out of sync in the 2nd level cache of Eclipselink.

An example workflow is:
- User performs a modifying transaction
- Eclipselink updates database correctly
- System is reading the updated entity again and shows data to user. The data presented to the user is the previous revision
- The user tries to perform the action again and now receives an OptimisticLockException
- The problem persists until we run `entityManager.getEntityManagerFactory().getCache().evictAll()` through an MBean.

There are a few frameworks involved as well as async sending of emails. However we haven't been able to reproduce the problem yet.

Has anyone experienced something similar or are there any ideas how to investigate this problem?

Thanks,
Thomas
Re: Unrecoverable OptimisticLockException [message #1840028 is a reply to message #1839854] Mon, 05 April 2021 15:26 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1388
Registered: July 2009
Senior Member
You mention EntityManager, so this must be a JPA implementation. Some questions:
1) "User performs a modifying transaction" is this JPQL update statements, triggers or entity modifications through JPA managed entities?
- details and the EclipseLink log messages for this transaction and its merge into the shared (EMF) cache would help with debugging this problem.

2) On "System is reading the updated entity again and shows data to user." how is this being done? (FindById, a query using getResultList etc)
2b) Is it in a new clear EntityManager context backed by the same EMF as committed the transaction, the same EntityManager or something entirely new or different VM?

3) What data is shown as stale (all of it, or only some fields)?
3b) does em.refresh or using the refresh query hint work around the issue?
This could be executed upfront, but certainly should be used when hitting an optimistic lock exception to force the stale data to be rebuilt from the database.
Re: Unrecoverable OptimisticLockException [message #1840037 is a reply to message #1840028] Tue, 06 April 2021 00:02 Go to previous message
Thomas Buckel is currently offline Thomas BuckelFriend
Messages: 2
Registered: March 2021
Junior Member
Hi Chris,

Thanks for your reply. Here are my answers as good as I can:

1) The entity is modified by entity modifications through JPA managed entities. Can you please suggest for which loggers you need debug output?

2) Typically the entity is loaded by EntityManager.find(Class,Id). Notable that the entity which is causing problems is also referenced by a @OneToMany collection in a parent entity. The system reads this parent entity often through a named query.
2b) A new clear EM context by the same EMF in the same VM. (we are using Spring/Spring Data)

3) Not sure. In the affected screens only some data is shown, but this is stale. Definitely @Version and a few other fields are stale.
3b) So far I've tried "QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache" on some key queries but with no effect.
Do you mean "eclipselink.refresh" as query hint?

I will try and implement an explicit refresh, but the problem is cropping up in quite a few place unfortunately. As a rule of thumb, do you suggest an EntityManager.refresh I've encountered an optimistic lock exception? Or should I even do an (inefficient) em.refresh() before I perform some of these failing operations?

Thanks for your help.

Cheers,
Thomas
Previous Topic:Load subentities with JPQL/EntitGraph is not working with EclipseLink
Next Topic:Create an array of objects when marshalled to json
Goto Forum:
  


Current Time: Sun Jun 13 18:50:19 GMT 2021

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

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

Back to the top