Skip to content

Commit

Permalink
OptimisticLockException while using L2 cache fix (#2250)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rfelcman authored Aug 26, 2024
1 parent 116033f commit 6de65af
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 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
Expand Down Expand Up @@ -177,4 +177,17 @@
</properties>
</persistence-unit>

<persistence-unit name="pu-with-dynamic-weaving" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.eclipse.persistence.testing.models.jpa.weave.IsolatedEntity</class>
<class>org.eclipse.persistence.testing.models.jpa.weave.Location</class>
<class>org.eclipse.persistence.testing.models.jpa.weave.Node</class>
<class>org.eclipse.persistence.testing.models.jpa.weave.Order</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="eclipselink.weaving" value="true"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2019, 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
Expand Down Expand Up @@ -607,4 +607,17 @@
</properties>
</persistence-unit>

<persistence-unit name="pu-with-dynamic-weaving" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.eclipse.persistence.testing.models.jpa.weave.IsolatedEntity</class>
<class>org.eclipse.persistence.testing.models.jpa.weave.Location</class>
<class>org.eclipse.persistence.testing.models.jpa.weave.Node</class>
<class>org.eclipse.persistence.testing.models.jpa.weave.Order</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="eclipselink.weaving" value="true"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--
Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2018, 2024 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -250,4 +250,17 @@
</properties>
</persistence-unit>

<persistence-unit name="pu-with-dynamic-weaving" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.eclipse.persistence.testing.models.jpa.weave.IsolatedEntity</class>
<class>org.eclipse.persistence.testing.models.jpa.weave.Location</class>
<class>org.eclipse.persistence.testing.models.jpa.weave.Node</class>
<class>org.eclipse.persistence.testing.models.jpa.weave.Order</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="eclipselink.weaving" value="true"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
package org.eclipse.persistence.testing.models.jpa.weave;

import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;

@Entity
@Table(name="JPA21_ISOLATED_ENTITY")
public class IsolatedEntity {

@Id
protected String id;

@Version
protected Integer version;

public IsolatedEntity() {
super();
}

public IsolatedEntity(String id) {
this.id = id;
}

public String getId() {
return id;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IsolatedEntity that = (IsolatedEntity) o;
return Objects.equals(id, that.id) && Objects.equals(version, that.version);
}

@Override
public int hashCode() {
return Objects.hash(id, version);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
package org.eclipse.persistence.testing.models.jpa.weave;

import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;

@Entity
@Table(name="JPA21_LOCATION")
public class Location {

@Id
protected Long id;

protected String locationId;

@ManyToOne(fetch = FetchType.LAZY)
protected Node node;

@Version
protected Integer version;

public Location(long id, String locationId) {
this.id = id;
this.locationId = locationId;
}

protected Location() {
}

public Long getId() {
return this.id;
}

public String getLocationId() {
return this.locationId;
}

public Node getNode() {
return node;
}

public void setNode(Node node) {
this.node = node;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Location location = (Location) o;
return Objects.equals(id, location.id) && Objects.equals(locationId, location.locationId) && Objects.equals(node, location.node) && Objects.equals(version, location.version);
}

@Override
public int hashCode() {
return Objects.hash(id, locationId, node, version);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
package org.eclipse.persistence.testing.models.jpa.weave;

import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;

@Entity
@Table(name="JPA21_NODE")
public class Node {

@Id
protected String id;

protected int availableBufferCapacity = 10;

@Version
protected Integer version;

public Node(String id) {
this.id = id;
}

protected Node() {
}

public String getId() {
return id;
}

public int getAvailableBufferCapacity() {
return availableBufferCapacity;
}

public void reserveBufferCapacity() {
availableBufferCapacity--;
}

public Integer getVersion() {
return version;
}

/*
public void setVersion(Integer version) {
this.version = version;
}
*/

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return availableBufferCapacity == node.availableBufferCapacity && Objects.equals(id, node.id) && Objects.equals(version, node.version);
}

@Override
public int hashCode() {
return Objects.hash(id, availableBufferCapacity, version);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
package org.eclipse.persistence.testing.models.jpa.weave;

import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;

@Entity
@Table(name="JPA21_ORDER_1")
public class Order {

@Id
protected Long id;

@ManyToOne(fetch = FetchType.LAZY)
protected Node node;

@Version
protected Integer version;

protected Order() {
}

public Order(long id) {

this.id = id;
}

public Long getId() {
return id;
}

public Node getNode() {
return node;
}

public void setNode(Node destinationNode) {
this.node = destinationNode;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Order order = (Order) o;
return Objects.equals(id, order.id) && Objects.equals(node, order.node) && Objects.equals(version, order.version);
}

@Override
public int hashCode() {
return Objects.hash(id, node, version);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2020 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -54,6 +54,7 @@
import org.eclipse.persistence.testing.tests.jpa.advanced.ReportQueryMultipleReturnTestSuite;
import org.eclipse.persistence.testing.tests.jpa.advanced.SQLResultSetMappingTestSuite;
import org.eclipse.persistence.testing.tests.jpa.advanced.UpdateAllQueryAdvancedJunitTest;
import org.eclipse.persistence.testing.tests.jpa.advanced.WeaveVersionTestSuite;
import org.eclipse.persistence.testing.tests.jpa.advanced.compositepk.AdvancedCompositePKJunitTest;
import org.eclipse.persistence.testing.tests.jpa.advanced.concurrency.ConcurrencyTest;
import org.eclipse.persistence.testing.tests.jpa.advanced.concurrency.LifecycleJUnitTest;
Expand Down Expand Up @@ -162,6 +163,7 @@ public static Test suite() {
suite.addTest(NamedQueryJUnitTest.suite());
suite.addTest(EntityEmbeddableTest.suite());
suite.addTest(InvalidNamedQueryTest.suite());
suite.addTest(WeaveVersionTestSuite.suite());
fullSuite.addTest(suite);

// Inheritance model.
Expand Down
Loading

0 comments on commit 6de65af

Please sign in to comment.