Stale Hibernate entities are found in some clustered nodes in EAP

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 5
    • 6
  • Hibernate
    • Hibernate 3 / JBossCache
    • Hibernate 4 / Infinispan

Issue

  • Clustering has been configured for multiple server JVMs
  • Level 2 caching is enabled for persistence units deployed in each server
  • When entities are concurrently accessed in multiple server JVMs, stale entities are randomly found in one or more JVMs
  • Once stale, entities remain in the level 2 cache until they are explicitly or implicitly (e.g. due to timeout) evicted

Resolution

One of the following approaches may be taken to address the issue

Root Cause

When a transaction commits changes for an entity it performs the following, in order:

  1. Execute SQL update against the database
  2. Publish invalidation message to all clustered servers
  3. Perform database commit

If logic running in a different server (from the transaction above) loads the entity from the database between steps 2 and 3 above (i.e. after evict has occurred locally in response to the invalidation message but before the commit completes) it is possible to load (or re-load) the entity in the exact state in which it exists prior to the database commit above.

Since no subsequent action takes place, the newly loaded (or re-loaded) entity becomes stale the instant the database commit initiated by the updating server completes. This entity will remain stale until evicted or explicitly refreshed.

Diagnostic Steps

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.