Skip to content

Commit f3e3404

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

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -22,6 +22,8 @@
2222
// - 389090: JPA 2.1 DDL Generation Support (foreign key metadata support)
2323
// 08/12/2015-2.6 Mythily Parthasarathy
2424
// - 474752: Address NPE for Embeddable with 1-M association
25+
// 03/03/2019-3.0 Alexandre Jacob
26+
// - 544995: Fixed batch fetching when shared cache is activated (multithreading issue)
2527
package org.eclipse.persistence.mappings;
2628

2729
import java.security.AccessController;
@@ -539,6 +541,9 @@ public Object extractResultFromBatchQuery(ReadQuery batchQuery, CacheKey parentC
539541
// If the object is already in the cache, then just return it.
540542
return cachedObject;
541543
}
544+
545+
boolean shouldExecuteBatchQuery = true;
546+
542547
// Ensure the query is only executed once.
543548
synchronized (batchQuery) {
544549
// Check if query was already executed.
@@ -636,17 +641,28 @@ public Object extractResultFromBatchQuery(ReadQuery batchQuery, CacheKey parentC
636641
originalPolicy.setDataResults(this, remainingParentRows);
637642
translationRow = translationRow.clone();
638643
translationRow.put(QUERY_BATCH_PARAMETER, foreignKeyValues);
644+
645+
if (foreignKeyValues.isEmpty()) {
646+
shouldExecuteBatchQuery = false;
647+
}
648+
639649
// Register each id as null, in case it has no relationship.
640650
for (Object foreignKey : foreignKeys) {
641651
batchedObjects.put(foreignKey, Helper.NULL_VALUE);
642652
}
643653
} else if (batchQuery.isReadAllQuery() && ((ReadAllQuery)batchQuery).getBatchFetchPolicy().isIN()) {
644654
throw QueryException.originalQueryMustUseBatchIN(this, originalQuery);
645655
}
646-
executeBatchQuery(batchQuery, parentCacheKey, batchedObjects, session, translationRow);
647-
batchQuery.setSession(null);
656+
657+
// Bug 544995 : When the cache was populated by another thread we can end up having nothing to fetch
658+
if (shouldExecuteBatchQuery) {
659+
executeBatchQuery(batchQuery, parentCacheKey, batchedObjects, session, translationRow);
660+
batchQuery.setSession(null);
661+
}
648662
}
663+
649664
result = batchedObjects.get(sourceKey);
665+
650666
if (result == Helper.NULL_VALUE) {
651667
return null;
652668
} else {

0 commit comments

Comments
 (0)