Skip to content

Commit 761fd34

Browse files
committed
[hibernate#2108] StatelessSession insertAll in batch does not do batching
1 parent f4bd7ad commit 761fd34

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinyStatelessSessionImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public Uni<Void> deleteAll(Object... entities) {
163163

164164
@Override
165165
public Uni<Void> deleteAll(int batchSize, Object... entities) {
166-
return uni( () -> delegate.reactiveDeleteAll( entities ) );
166+
return uni( () -> delegate.reactiveDeleteAll( batchSize, entities ) );
167167
}
168168

169169
@Override

hibernate-reactive-core/src/main/java/org/hibernate/reactive/session/ReactiveStatelessSession.java

+4
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,8 @@ public interface ReactiveStatelessSession extends ReactiveQueryProducer, Reactiv
7777
void close(CompletableFuture<Void> closing);
7878

7979
Object getIdentifier(Object entity);
80+
81+
void setJdbcBatchSize(Integer jdbcBatchSize);
82+
83+
Integer getJdbcBatchSize();
8084
}

hibernate-reactive-core/src/main/java/org/hibernate/reactive/session/impl/ReactiveStatelessSessionImpl.java

+22-9
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,12 @@ private ReactiveStatelessSessionImpl(
150150
PersistenceContext persistenceContext) {
151151
super( factory, options );
152152
this.persistenceContext = persistenceContext;
153-
Integer batchSize = getConfiguredJdbcBatchSize();
154-
reactiveConnection = batchSize == null || batchSize < 2
155-
? connection
156-
: new BatchingConnection( connection, batchSize );
153+
// Don't know why ORM set the batch size to 0 in the super constructor,
154+
// but the default should be the one in the factory options
155+
int defaultBatchSize = factory.getSessionFactoryOptions().getJdbcBatchSize();
156+
setJdbcBatchSize( defaultBatchSize );
157+
// BatchingConnection will use the original connection if defaultBatchSize <= 1
158+
reactiveConnection = new BatchingConnection( connection, defaultBatchSize );
157159
batchingHelperSession = this;
158160
influencers = new LoadQueryInfluencers( factory );
159161
}
@@ -551,9 +553,12 @@ public CompletionStage<Void> reactiveInsertAll(Object... entities) {
551553

552554
@Override
553555
public CompletionStage<Void> reactiveInsertAll(int batchSize, Object... entities) {
556+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
557+
batchingHelperSession.setJdbcBatchSize( batchSize );
554558
final ReactiveConnection connection = batchingConnection( batchSize );
555559
return loop( entities, batchingHelperSession::reactiveInsert )
556-
.thenCompose( v -> connection.executeBatch() );
560+
.thenCompose( v -> connection.executeBatch() )
561+
.whenComplete( (v, throwable) -> batchingHelperSession.setJdbcBatchSize( jdbcBatchSize ) );
557562
}
558563

559564
@Override
@@ -564,9 +569,12 @@ public CompletionStage<Void> reactiveUpdateAll(Object... entities) {
564569

565570
@Override
566571
public CompletionStage<Void> reactiveUpdateAll(int batchSize, Object... entities) {
572+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
573+
batchingHelperSession.setJdbcBatchSize( batchSize );
567574
final ReactiveConnection connection = batchingConnection( batchSize );
568575
return loop( entities, batchingHelperSession::reactiveUpdate )
569-
.thenCompose( v -> connection.executeBatch() );
576+
.thenCompose( v -> connection.executeBatch() )
577+
.whenComplete( (v, throwable) -> batchingHelperSession.setJdbcBatchSize( jdbcBatchSize ) );
570578
}
571579

572580
@Override
@@ -577,9 +585,11 @@ public CompletionStage<Void> reactiveDeleteAll(Object... entities) {
577585

578586
@Override
579587
public CompletionStage<Void> reactiveDeleteAll(int batchSize, Object... entities) {
588+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
589+
batchingHelperSession.setJdbcBatchSize( batchSize );
580590
final ReactiveConnection connection = batchingConnection( batchSize );
581-
return loop( entities, batchingHelperSession::reactiveDelete )
582-
.thenCompose( v -> connection.executeBatch() );
591+
return loop( entities, batchingHelperSession::reactiveDelete ).thenCompose( v -> connection.executeBatch() )
592+
.whenComplete( (v, throwable) -> batchingHelperSession.setJdbcBatchSize( jdbcBatchSize ) );
583593
}
584594

585595

@@ -591,9 +601,12 @@ public CompletionStage<Void> reactiveRefreshAll(Object... entities) {
591601

592602
@Override
593603
public CompletionStage<Void> reactiveRefreshAll(int batchSize, Object... entities) {
604+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
605+
batchingHelperSession.setJdbcBatchSize( batchSize );
594606
final ReactiveConnection connection = batchingConnection( batchSize );
595607
return loop( entities, batchingHelperSession::reactiveRefresh )
596-
.thenCompose( v -> connection.executeBatch() );
608+
.thenCompose( v -> connection.executeBatch() )
609+
.whenComplete( (v, throwable) -> batchingHelperSession.setJdbcBatchSize( jdbcBatchSize ) );
597610
}
598611

599612
private ReactiveConnection batchingConnection(int batchSize) {

0 commit comments

Comments
 (0)