-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes coalesce and case operators in multithreaded environments with different number of arguments #2259
Merged
Merged
Fixes coalesce and case operators in multithreaded environments with different number of arguments #2259
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
.../org/eclipse/persistence/jpa/test/jpql/TestArgumentListFunctionExpressionConcurrency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* Copyright (c) 2024 IBM Corporation. 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.jpa.test.jpql; | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.ObjIntConsumer; | ||
|
||
import org.eclipse.persistence.jpa.test.framework.DDLGen; | ||
import org.eclipse.persistence.jpa.test.framework.Emf; | ||
import org.eclipse.persistence.jpa.test.framework.EmfRunner; | ||
import org.eclipse.persistence.jpa.test.jpql.model.JPQLEntity; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.EntityManagerFactory; | ||
|
||
/** | ||
* This test reproduces the issues #2136, #1867 and #1717. | ||
* | ||
* @author Igor Mukhin | ||
*/ | ||
@RunWith(EmfRunner.class) | ||
public class TestArgumentListFunctionExpressionConcurrency { | ||
|
||
private static final int MAX_THREADS = Math.min(Runtime.getRuntime().availableProcessors(), 4); | ||
private static final int ITERATIONS_PER_THREAD = 1000; | ||
|
||
@Emf(name = "argumentListFunctionExpressionConcurrencyEMF", createTables = DDLGen.DROP_CREATE, classes = { JPQLEntity.class }) | ||
private EntityManagerFactory emf; | ||
|
||
@Test | ||
public void testConcurrentUseOfCoalesce() throws Exception { | ||
runInParallel((em, i) -> { | ||
String jpql; | ||
if (i % 2 == 0) { | ||
jpql = "SELECT p FROM JPQLEntity p WHERE p.string1 = coalesce(p.string2, p.string1, '" + cacheBuster(i) + "')"; | ||
} else { | ||
jpql = "SELECT p FROM JPQLEntity p WHERE p.string1 = coalesce(p.string2, '" + cacheBuster(i) + "')"; | ||
} | ||
em.createQuery(jpql, JPQLEntity.class).getResultList(); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testConcurrentUseOfCaseCondition() throws Exception { | ||
runInParallel((em, i) -> { | ||
String jpql; | ||
if (i % 2 == 0) { | ||
jpql = "SELECT p FROM JPQLEntity p" | ||
+ " WHERE p.string1 = case " | ||
+ " when p.string2 = '" + cacheBuster(i) + "' then p.string1 " | ||
+ " else null " | ||
+ " end"; | ||
} else { | ||
jpql = "SELECT p FROM JPQLEntity p" | ||
+ " WHERE p.string1 = case " | ||
+ " when p.string2 = '" + cacheBuster(i) + "' then p.string1" | ||
+ " when p.string2 = 'x' then p.string2" | ||
+ " else null " | ||
+ " end"; | ||
|
||
} | ||
em.createQuery(jpql, JPQLEntity.class).getResultList(); | ||
}); | ||
} | ||
|
||
private static String cacheBuster(Integer i) { | ||
return "cacheBuster." + Thread.currentThread().getName() + "." + i; | ||
} | ||
|
||
private void runInParallel(ObjIntConsumer<EntityManager> runnable) throws Exception { | ||
AtomicReference<Exception> exception = new AtomicReference<>(); | ||
|
||
// start all threads | ||
List<Thread> threads = new ArrayList<>(); | ||
for (int t = 0; t < MAX_THREADS; t++) { | ||
Thread thread = new Thread(() -> { | ||
try { | ||
for (int i = 0; i < ITERATIONS_PER_THREAD; i++) { | ||
if (exception.get() != null) { | ||
return; | ||
} | ||
|
||
try (EntityManager em = emf.createEntityManager()) { | ||
runnable.accept(em, i); | ||
} | ||
|
||
} | ||
} catch (Exception e) { | ||
exception.set(e); | ||
} | ||
}); | ||
threads.add(thread); | ||
thread.start(); | ||
} | ||
|
||
// wait for all threads to finish | ||
threads.forEach(thread -> { | ||
try { | ||
thread.join(); | ||
} catch (InterruptedException e) { | ||
exception.set(e); | ||
} | ||
}); | ||
|
||
// throw the first exception that occurred | ||
if (exception.get() != null) { | ||
throw exception.get(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry but this removal is problematic as there should be some differences between DB platforms.
Try e.g.
mvn clean verify -pl :org.eclipse.persistence.jpa.jse.test -P derby
against before modification and after to see what happens.-P derby
means that Implicit Apache Derby DB is used (it is managed started/stopped by EclipseLink Maven build scripts).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I see what you mean.
In that case the problem is that getPlatformOperator() returns the same cached instance of the operator on each call which leads to errors with the operators with variable number of arguments like "coalesce" and "case when".
I think I have a solution in mind. I will push it later.