Skip to content

Commit 7063f49

Browse files
committed
Bug 544995 : Fixed batch fetching when shared cache is activated (multithreading issue)
Signed-off-by: Alexandre Jacob <[email protected]>
1 parent f9b6559 commit 7063f49

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/mappings/ForeignReferenceMapping.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
// - 389090: JPA 2.1 DDL Generation Support (foreign key metadata support)
2424
// 08/12/2015-2.6 Mythily Parthasarathy
2525
// - 474752: Address NPE for Embeddable with 1-M association
26+
// 04/19/2020-3.0 Alexandre Jacob
27+
// - 544995: Fixed batch fetching when shared cache is activated (multithreading issue)
2628
package org.eclipse.persistence.mappings;
2729

2830
import java.security.AccessController;
@@ -540,6 +542,9 @@ public Object extractResultFromBatchQuery(ReadQuery batchQuery, CacheKey parentC
540542
// If the object is already in the cache, then just return it.
541543
return cachedObject;
542544
}
545+
546+
boolean shouldExecuteBatchQuery = true;
547+
543548
// Ensure the query is only executed once.
544549
synchronized (batchQuery) {
545550
// Check if query was already executed.
@@ -637,15 +642,24 @@ public Object extractResultFromBatchQuery(ReadQuery batchQuery, CacheKey parentC
637642
originalPolicy.setDataResults(this, remainingParentRows);
638643
translationRow = translationRow.clone();
639644
translationRow.put(QUERY_BATCH_PARAMETER, foreignKeyValues);
645+
646+
if (foreignKeyValues.isEmpty()) {
647+
shouldExecuteBatchQuery = false;
648+
}
649+
640650
// Register each id as null, in case it has no relationship.
641651
for (Object foreignKey : foreignKeys) {
642652
batchedObjects.put(foreignKey, Helper.NULL_VALUE);
643653
}
644654
} else if (batchQuery.isReadAllQuery() && ((ReadAllQuery)batchQuery).getBatchFetchPolicy().isIN()) {
645655
throw QueryException.originalQueryMustUseBatchIN(this, originalQuery);
646656
}
647-
executeBatchQuery(batchQuery, parentCacheKey, batchedObjects, session, translationRow);
648-
batchQuery.setSession(null);
657+
658+
// Bug 544995 : When the cache is populated by another thread we can end up having nothing to fetch
659+
if (shouldExecuteBatchQuery) {
660+
executeBatchQuery(batchQuery, parentCacheKey, batchedObjects, session, translationRow);
661+
batchQuery.setSession(null);
662+
}
649663
}
650664
result = batchedObjects.get(sourceKey);
651665
if (result == Helper.NULL_VALUE) {

0 commit comments

Comments
 (0)