From 6de65afa2ab627faace8a742846b0c5e5987798c Mon Sep 17 00:00:00 2001 From: Radek Felcman Date: Mon, 26 Aug 2024 11:06:41 +0200 Subject: [PATCH] OptimisticLockException while using L2 cache fix (#2250) Under specific conditions is org.eclipse.persistence.exceptions.OptimisticLockException incorrectly thrown. Environment conditions are: JPA L2 cache enabled Weaving is applied to used entities @Version annotation is used Test org.eclipse.persistence.testing.tests.advanced2.weave.WeaveVersionTest#testWeavedEntitiesWithVersionL2Cache describe sequence of steps which leads into org.eclipse.persistence.exceptions.OptimisticLockException if fix is not applied. Purpose of fix in org.eclipse.persistence.internal.sessions.UnitOfWorkImpl#cloneAndRegisterObject(java.lang.Object, org.eclipse.persistence.internal.identitymaps.CacheKey, org.eclipse.persistence.internal.identitymaps.CacheKey, org.eclipse.persistence.descriptors.ClassDescriptor) is update current working object with non-invalidated version from UnitOfWork scope if original is still invalid. Signed-off-by: Radek Felcman --- .../internal/sessions/UnitOfWorkImpl.java | 5 +- .../osgi/persistence.xml | 15 +- .../persistence.xml | 15 +- .../spring/persistence.xml | 15 +- .../models/jpa/weave/IsolatedEntity.java | 55 +++++ .../testing/models/jpa/weave/Location.java | 74 +++++++ .../testing/models/jpa/weave/Node.java | 74 +++++++ .../testing/models/jpa/weave/Order.java | 68 ++++++ .../tests/jpa/FullRegressionTestSuite.java | 4 +- .../jpa/advanced/WeaveVersionTestSuite.java | 208 ++++++++++++++++++ 10 files changed, 528 insertions(+), 5 deletions(-) create mode 100644 jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/models/jpa/weave/IsolatedEntity.java create mode 100644 jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/models/jpa/weave/Location.java create mode 100644 jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/models/jpa/weave/Node.java create mode 100644 jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/models/jpa/weave/Order.java create mode 100644 jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/advanced/WeaveVersionTestSuite.java diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/sessions/UnitOfWorkImpl.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/sessions/UnitOfWorkImpl.java index 3e03ac850a2..d5f8fb43134 100644 --- a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/sessions/UnitOfWorkImpl.java +++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/sessions/UnitOfWorkImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019 IBM Corporation. All rights reserved. * * This program and the accompanying materials are made available under the @@ -1078,6 +1078,9 @@ public Object cloneAndRegisterObject(Object original, CacheKey parentCacheKey, C } } try { + if (isConsideredInvalid(original, parentCacheKey, descriptor) && unitOfWorkCacheKey.getObject() != null) { + original = unitOfWorkCacheKey.getObject(); + } // bug:6167576 Must acquire the lock before cloning. workingClone = builder.instantiateWorkingCopyClone(original, this); // PERF: Cache the primary key if implements PersistenceEntity. diff --git a/jpa/eclipselink.jpa.test/resource/eclipselink-annotation-model/osgi/persistence.xml b/jpa/eclipselink.jpa.test/resource/eclipselink-annotation-model/osgi/persistence.xml index a35ea465a8f..c88cff98351 100644 --- a/jpa/eclipselink.jpa.test/resource/eclipselink-annotation-model/osgi/persistence.xml +++ b/jpa/eclipselink.jpa.test/resource/eclipselink-annotation-model/osgi/persistence.xml @@ -1,6 +1,6 @@