The soft-locked cache entry has already been deleted. Off-balance locking / unlocking?

I am using java 1.6, spring 2.5, hibernate 3.3.1 and ehcache 2.6.0. Programs are connected to two databases. There are two ehcache configurations, but only one is used in this case. At the end of the package, programs return this error:

2012-10-23 15:44:43,406 ERROR (AbstractReadWriteEhcacheAccessStrategy.java:159) - Cache dao.data.MyObject Key dao.data.MyObject#28 Lockable : null A soft-locked cache entry was removed already. Out of balance lock/unlock sequences ? 

What can cause this error?

+6
source share
2 answers

Now it amazes me, so I will share my conclusions.

First, some background:

  • We use Hibernate 3.5.2 and EhCache 2.7.0
  • There is only one database (the other is read-only, but it does not use caching because it is updated externally) and an instance of a single cache (without clustering)
  • The problem occurs when updating an object associated with InheritanceType.JOINED and CacheConcurrencyStrategy.READ_WRITE (but not when switching to NONSTRICT_READ_WRITE). In addition, judging by the verification of the code, this will happen during deletion.

What I found when debugging Hibernate and EhCache code:

  • Hibernate EntityUpdateAction (in execute ()) calls EntityRegionAccessStrategy.lockItem ()
  • An EhCache implementation (in AbstractReadWriteEhcacheAccessStrategy) changes the mapping for a given key from a cached object to a lock
  • Hibernate detects that an object maps to more than one table, and therefore invalidation caching is required (see AbstractEntityPersister.isCacheInvalidationRequired ())
  • It goes to the persister.getCacheAccessStrategy () call. remove (), which in the EhCache implementation removes the mapping for the given cache key. However, while Hibernate expects this to actually delete the cached object, with EhCache it will remove the lock (which was placed there in step 2).
  • After the transaction is completed in EntityUpdateAction.doAfterTransactionCompletion (), Hibernate detects that a cache invalidation is required, and proceeds to calling unlockItem (), which does not work if there is no mapping for the specified cache key, as a result of which you described an error message

It seems to me that this is a problem in the implementation of EhCache lockItem () and unlockItem () (in the case of READ_WRITE). It should not replace the actual elements with locks, but store locks separately. At least you could say that Hibernate and EhCache are not 100% compatible in this scenario.

A few final notes:

  • This error seems to be harmless and can be safely suppressed using the log4j configuration.
  • I managed to repeat this in another scenario (which boils down to the same lock / delete / unlock sequence). This time, instead of shared inheritance, the original SQL triggered the deletion. Everything else is almost the same.
  • The corresponding bits of the Hibernate and EhCache code are: EntityRegionAccessStrategy, EntityAction, CollectionRegionAccessStrategy, CollectionAction and their associated implementations / extensions of specific classes.

NTN

+3
source

I have the same problem when I used createSQLQuery (Native SQL Query). Thanks to this, the Impact of native sql queries in the second level hibernate cache I fixed my problem.

From the link: The main reason: your own queries can lead to invalidation of second-level caches. There is permission at the bottom of the link to prevent hibernation from invalidating the L2 cache when using your own SQL query.

+2
source

All Articles