From bead3af2b3ef859ff90157aa4af29cb88e6d6012 Mon Sep 17 00:00:00 2001 From: Eddie Carpenter Date: Mon, 26 Aug 2024 15:19:44 +1200 Subject: [PATCH] Added a precautionary removal of a cached record when there is an optimistic lock failure. --- .../org/jpalite/impl/db/PersistenceContextImpl.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/jpalite-core/src/main/java/org/jpalite/impl/db/PersistenceContextImpl.java b/jpalite-core/src/main/java/org/jpalite/impl/db/PersistenceContextImpl.java index 0268bb1..b3bcbb3 100644 --- a/jpalite-core/src/main/java/org/jpalite/impl/db/PersistenceContextImpl.java +++ b/jpalite-core/src/main/java/org/jpalite/impl/db/PersistenceContextImpl.java @@ -382,7 +382,7 @@ public void setConnectionName(String connectionName) this.connectionName = connectionName; } - @SuppressWarnings({"java:S1141", "java:S2077", "tainting"}) + @SuppressWarnings({"java:S1141", "java:S2077"}) //Having try-resource in a bigger try block is allowed. Dynamically formatted SQL is verified to be safe @Override @Nonnull @@ -804,6 +804,15 @@ else if (entity._getMetaData().isCacheable() && cacheStoreMode != CacheStoreMode */ else if (action != INSERT && isOptimisticLocked(entity)) { setRollbackOnly(); + + /* + Delete the cached record for the rare case where a cached record might be out of date. + NOTE: This is highly unlikely and an error in itself. + */ + if (entity._getMetaData().isCacheable()) { + l2Cache().evict(entity._getEntityClass(), entity._getPrimaryKey()); + }//if + throw new OptimisticLockException(entity); }//else if }//try