Skip to content

Commit 0e99eb7

Browse files
committed
[#2108] StatelessSession insertAll in batch does not do batching
1 parent 98b3e22 commit 0e99eb7

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

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

+44-13
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,12 @@ private ReactiveStatelessSessionImpl(
150150
PersistenceContext persistenceContext) {
151151
super( factory, options );
152152
this.persistenceContext = persistenceContext;
153+
// StatelessSessionImpl constructor sets the Jdbc Batch Size to 0,
154+
// setting it to null allows getConfiguredJdbcBatchSize() to return correct configured size
155+
setJdbcBatchSize( null );
153156
Integer batchSize = getConfiguredJdbcBatchSize();
154157
reactiveConnection = batchSize == null || batchSize < 2
155-
? connection
158+
? new BatchingConnection( connection, 1 )
156159
: new BatchingConnection( connection, batchSize );
157160
batchingHelperSession = this;
158161
influencers = new LoadQueryInfluencers( factory );
@@ -551,9 +554,16 @@ public CompletionStage<Void> reactiveInsertAll(Object... entities) {
551554

552555
@Override
553556
public CompletionStage<Void> reactiveInsertAll(int batchSize, Object... entities) {
554-
final ReactiveConnection connection = batchingConnection( batchSize );
555-
return loop( entities, batchingHelperSession::reactiveInsert )
556-
.thenCompose( v -> connection.executeBatch() );
557+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
558+
batchingHelperSession.setJdbcBatchSize( batchSize );
559+
try {
560+
final ReactiveConnection connection = batchingConnection( batchSize );
561+
return loop( entities, batchingHelperSession::reactiveInsert )
562+
.thenCompose( v -> connection.executeBatch() );
563+
}
564+
finally {
565+
batchingHelperSession.setJdbcBatchSize( jdbcBatchSize );
566+
}
557567
}
558568

559569
@Override
@@ -564,9 +574,16 @@ public CompletionStage<Void> reactiveUpdateAll(Object... entities) {
564574

565575
@Override
566576
public CompletionStage<Void> reactiveUpdateAll(int batchSize, Object... entities) {
567-
final ReactiveConnection connection = batchingConnection( batchSize );
568-
return loop( entities, batchingHelperSession::reactiveUpdate )
569-
.thenCompose( v -> connection.executeBatch() );
577+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
578+
batchingHelperSession.setJdbcBatchSize( batchSize );
579+
try {
580+
final ReactiveConnection connection = batchingConnection( batchSize );
581+
return loop( entities, batchingHelperSession::reactiveUpdate )
582+
.thenCompose( v -> connection.executeBatch() );
583+
}
584+
finally {
585+
batchingHelperSession.setJdbcBatchSize( jdbcBatchSize );
586+
}
570587
}
571588

572589
@Override
@@ -577,9 +594,16 @@ public CompletionStage<Void> reactiveDeleteAll(Object... entities) {
577594

578595
@Override
579596
public CompletionStage<Void> reactiveDeleteAll(int batchSize, Object... entities) {
580-
final ReactiveConnection connection = batchingConnection( batchSize );
581-
return loop( entities, batchingHelperSession::reactiveDelete )
582-
.thenCompose( v -> connection.executeBatch() );
597+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
598+
batchingHelperSession.setJdbcBatchSize( batchSize );
599+
try {
600+
final ReactiveConnection connection = batchingConnection( batchSize );
601+
return loop( entities, batchingHelperSession::reactiveDelete )
602+
.thenCompose( v -> connection.executeBatch() );
603+
}
604+
finally {
605+
batchingHelperSession.setJdbcBatchSize( jdbcBatchSize );
606+
}
583607
}
584608

585609

@@ -591,9 +615,16 @@ public CompletionStage<Void> reactiveRefreshAll(Object... entities) {
591615

592616
@Override
593617
public CompletionStage<Void> reactiveRefreshAll(int batchSize, Object... entities) {
594-
final ReactiveConnection connection = batchingConnection( batchSize );
595-
return loop( entities, batchingHelperSession::reactiveRefresh )
596-
.thenCompose( v -> connection.executeBatch() );
618+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
619+
batchingHelperSession.setJdbcBatchSize( batchSize );
620+
try {
621+
final ReactiveConnection connection = batchingConnection( batchSize );
622+
return loop( entities, batchingHelperSession::reactiveRefresh )
623+
.thenCompose( v -> connection.executeBatch() );
624+
}
625+
finally {
626+
batchingHelperSession.setJdbcBatchSize( jdbcBatchSize );
627+
}
597628
}
598629

599630
private ReactiveConnection batchingConnection(int batchSize) {

0 commit comments

Comments
 (0)