Skip to content

Commit

Permalink
Testcase for bug 35468915
Browse files Browse the repository at this point in the history
Signed-off-by: Vaibhav Vishal <[email protected]>
  • Loading branch information
vavishal committed Aug 31, 2023
1 parent a7cb02e commit 8a06324
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--
Copyright (c) 2018, 2022 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2018, 2023 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 @@ -55,4 +55,21 @@
<property name="eclipselink.logging.parameters" value="${eclipselink.logging.parameters}"/>
</properties>
</persistence-unit>
<persistence-unit name="PU-BatchWriting" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.sessions-xml" value="META-INF/sessions.xml"/>
<property name="eclipselink.session-name" value="BatchWritingSession"/>
<property name="eclipselink.exception-handler" value="org.eclipse.persistence.testing.tests.jpa.jpaadvancedproperties.CustomizedExceptionHandler"/>
<property name="eclipselink.session-event-listener" value="org.eclipse.persistence.testing.tests.jpa.jpaadvancedproperties.CustomizedSessionEventListener"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<property name="eclipselink.logging.logger" value="JavaLogger"/>
<property name="eclipselink.logging.file" value="JPAAdvancedProperties.log"/>
<property name="eclipselink.profiler" value="NoProfiler"/>
<property name="eclipselink.logging.level" value="${eclipselink.logging.level}"/>
<property name="eclipselink.logging.level.sql" value="${eclipselink.logging.sql.level}"/>
<property name="eclipselink.logging.parameters" value="${eclipselink.logging.parameters}"/>
</properties>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="US-ASCII"?>
<!--
Copyright (c) 2018, 2022 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2018, 2023 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 All @@ -19,4 +19,9 @@
<logging xsi:type="java-log"/>
<primary-project xsi:type="xml">META-INF/RelationshipProject.xml</primary-project>
</session>
<session xsi:type="server-session">
<name>BatchWritingSession</name>
<logging xsi:type="java-log"/>
<primary-project xsi:type="xml">META-INF/RelationshipProject.xml</primary-project>
</session>
</sessions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 1998, 2023 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.tests.jpa.jpaadvancedproperties;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;

import org.junit.Assert;
import org.eclipse.persistence.exceptions.OptimisticLockException;
import org.eclipse.persistence.testing.framework.jpa.junit.JUnitTestCase;
import org.eclipse.persistence.testing.models.jpa.jpaadvancedproperties.Customer;
import org.eclipse.persistence.testing.models.jpa.jpaadvancedproperties.ModelExamples;

public class OptimisticLockingBatchWritingTest extends JUnitTestCase {

public OptimisticLockingBatchWritingTest() {
}

public OptimisticLockingBatchWritingTest(String name) {
super(name);
setPuName(getPersistenceUnitName());
}

@Override
public String getPersistenceUnitName() {
return "PU-BatchWriting";
}

public void testSingleStatementBatch() {
EntityManager em = createEntityManager();
try {
beginTransaction(em);
Customer customer = ModelExamples.customerExample1();
em.persist(customer);
Integer customerId = customer.getCustomerId();
commitTransaction(em);
callTestImpl(customerId, new Integer[] { customerId });
} catch (RuntimeException e) {
if (isTransactionActive(em)){
rollbackTransaction(em);
}
throw e;
}finally{
closeEntityManager(em);
}
}

public void testMultipleStatementsBatch() {
EntityManager em = createEntityManager();
try {
beginTransaction(em);
Customer customer1 = ModelExamples.customerExample1();
em.persist(customer1);
Integer customerId1 = customer1.getCustomerId();
Customer customer2 = ModelExamples.customerExample2();
em.persist(customer2);
Integer customerId2 = customer2.getCustomerId();
commitTransaction(em);
callTestImpl(customerId2, new Integer[] { customerId1, customerId2 });
} catch (RuntimeException e) {
if (isTransactionActive(em)){
rollbackTransaction(em);
}
throw e;
}finally{
closeEntityManager(em);
}
}

protected void callTestImpl(Integer customerId, Integer[] customerIds) {

new Thread(() -> changeOrderAndWaitInTransaction(500, new Integer[] { customerId })).start();

try {
changeOrderAndWaitInTransaction(1000, customerIds);
Assert.fail("There should have been an optimistic lock");
} catch (Throwable e) {
Assert.assertTrue(e.getMessage(),
e.getMessage().contains("One or more objects of class"));
}

}

protected void changeOrderAndWaitInTransaction(int sleepInMillis, Integer[] customerIds) {
EntityManager em = createEntityManager();
try {
beginTransaction(em);
for (Integer customerId : customerIds) {
Customer customer = em.find(org.eclipse.persistence.testing.models.jpa.jpaadvancedproperties.Customer.class, customerId);
customer.setName(customer.getName() + "-modified");
}
try {
Thread.sleep(sleepInMillis);
} catch (InterruptedException e) {
// ignore for this test
}

commitTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)){
rollbackTransaction(em);
}
throw e;
}finally{
closeEntityManager(em);
}

}

}

0 comments on commit 8a06324

Please sign in to comment.