From 0f1d29252759fe40ed1e6fad19b7835e8326bfd1 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 15 Apr 2021 12:50:03 +0200 Subject: [PATCH 1/2] proposed fix for #707 make sure that queued requests use the right session and entity args --- .../reactive/id/impl/BlockingIdentifierGenerator.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/impl/BlockingIdentifierGenerator.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/impl/BlockingIdentifierGenerator.java index e02108cb2..e30112d4b 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/impl/BlockingIdentifierGenerator.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/impl/BlockingIdentifierGenerator.java @@ -38,7 +38,7 @@ public abstract class BlockingIdentifierGenerator implements ReactiveIdentifierG private int loValue; private long hiValue; - private volatile List> queue = null; + private volatile List queue = null; protected synchronized long next() { return loValue>0 && loValue generate(ReactiveConnectionSupplier session, Object // go off and fetch the next hi value from db nextHiValue(session).thenAccept( id -> { // Vertx.currentContext().runOnContext(v -> { - List> list; + List list; synchronized (this) { // clone ref to the queue list = queue; @@ -79,14 +79,14 @@ public CompletionStage generate(ReactiveConnectionSupplier session, Object result.complete( next(id) ); } // send waiting streams back to try again - list.forEach( completion -> generate(session, entity) - .thenAccept(completion::complete) ); + list.forEach(Runnable::run); // } ); } ); } else { // wait for the concurrent fetch to complete - queue.add(result); + // note that we carefully capture the right session,entity here! + queue.add( () -> generate(session, entity).thenAccept(result::complete) ); } return result; } From 09f2d74014c1c8dc92fb4d649b052ca390fcee03 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 15 Apr 2021 13:35:22 +0200 Subject: [PATCH 2/2] when we're not using blocking, just go direct to the database related to discussion around #707 This seems right for sequences. I'm not sure about tables though. --- .../reactive/id/impl/BlockingIdentifierGenerator.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/impl/BlockingIdentifierGenerator.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/impl/BlockingIdentifierGenerator.java index e30112d4b..02cb88b5a 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/impl/BlockingIdentifierGenerator.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/impl/BlockingIdentifierGenerator.java @@ -54,6 +54,11 @@ protected synchronized long next(long hi) { @Override public CompletionStage generate(ReactiveConnectionSupplier session, Object entity) { + if ( getBlockSize()<=1 ) { + //special case where we're not using blocking at all + return nextHiValue(session); + } + long local = next(); if ( local >= 0 ) { // We don't need to update or initialize the hi