Skip to content

Commit 0882fae

Browse files
committed
proposed fix for #707
make sure that queued requests use the right session and entity args
1 parent d178627 commit 0882fae

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/impl/BlockingIdentifierGenerator.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,27 @@ public abstract class BlockingIdentifierGenerator implements ReactiveIdentifierG
3838
private int loValue;
3939
private long hiValue;
4040

41-
private volatile List<CompletableFuture<Long>> queue = null;
41+
private volatile List<Queued> queue = null;
42+
43+
/**
44+
* A queued request for an identifier that can't be satisfied immediately
45+
* because we're currently fetching a new "hi" value in a different request.
46+
*/
47+
private final class Queued {
48+
private final ReactiveConnectionSupplier session;
49+
private final Object entity;
50+
private final CompletableFuture<Long> completion;
51+
52+
private Queued(ReactiveConnectionSupplier session, Object entity, CompletableFuture<Long> completion) {
53+
this.session = session;
54+
this.entity = entity;
55+
this.completion = completion;
56+
}
57+
58+
private void complete() {
59+
generate(session, entity).thenAccept(completion::complete);
60+
}
61+
}
4262

4363
protected synchronized long next() {
4464
return loValue>0 && loValue<getBlockSize()
@@ -70,7 +90,7 @@ public CompletionStage<Long> generate(ReactiveConnectionSupplier session, Object
7090
// go off and fetch the next hi value from db
7191
nextHiValue(session).thenAccept( id -> {
7292
// Vertx.currentContext().runOnContext(v -> {
73-
List<CompletableFuture<Long>> list;
93+
List<Queued> list;
7494
synchronized (this) {
7595
// clone ref to the queue
7696
list = queue;
@@ -79,14 +99,13 @@ public CompletionStage<Long> generate(ReactiveConnectionSupplier session, Object
7999
result.complete( next(id) );
80100
}
81101
// send waiting streams back to try again
82-
list.forEach( completion -> generate(session, entity)
83-
.thenAccept(completion::complete) );
102+
list.forEach(Queued::complete);
84103
// } );
85104
} );
86105
}
87106
else {
88107
// wait for the concurrent fetch to complete
89-
queue.add(result);
108+
queue.add( new Queued(session, entity, result) );
90109
}
91110
return result;
92111
}

0 commit comments

Comments
 (0)