Skip to content

Commit eaeda2f

Browse files
committed
HHH-12086 - Batch order_inserts: flush during transaction causes incorrect insert ordering and subsequent constraint violation
Add a maximum iteration threshold, to avoid a stack overflow error in case there will be circular dependency that's not properly detected
1 parent a5d50c3 commit eaeda2f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,12 +1166,17 @@ public void sort(List<AbstractEntityInsertAction> insertions) {
11661166
}
11671167
}
11681168

1169-
boolean stored = false;
1169+
boolean sorted = false;
1170+
1171+
long maxIterations = latestBatches.size() * 2;
1172+
long iterations = 0;
11701173

11711174
sort:
11721175
do {
11731176
// Examine each entry in the batch list, sorting them based on parent/child association
11741177
// as depicted by the dependency graph.
1178+
iterations++;
1179+
11751180
for ( int i = 0; i < latestBatches.size(); i++ ) {
11761181
BatchIdentifier batchIdentifier = latestBatches.get( i );
11771182

@@ -1204,9 +1209,14 @@ public void sort(List<AbstractEntityInsertAction> insertions) {
12041209
}
12051210
}
12061211
}
1207-
stored = true;
1212+
sorted = true;
1213+
}
1214+
while ( !sorted && iterations <= maxIterations);
1215+
1216+
if ( iterations > maxIterations ) {
1217+
LOG.warn( "The batch containing " + latestBatches.size() + " statements could not be sorted after " + maxIterations + " iterations. " +
1218+
"This might indicate a bug in Hibernate!" );
12081219
}
1209-
while ( !stored );
12101220

12111221
// Now, rebuild the insertions list. There is a batch for each entry in the name list.
12121222
for ( BatchIdentifier rootIdentifier : latestBatches ) {

0 commit comments

Comments
 (0)