Skip to content

Commit 3378325

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

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,8 @@ default <T> Uni<T> get(Class<T> entityClass, Object id, LockModeType lockModeTyp
17791779
Uni<Void> insert(Object entity);
17801780

17811781
/**
1782-
* Insert multiple rows.
1782+
* Insert multiple rows, using the number of the
1783+
* given entities as the batch size.
17831784
*
17841785
* @param entities new transient instances
17851786
*

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public Uni<Void> insert(Object entity) {
138138

139139
@Override
140140
public Uni<Void> insertAll(Object... entities) {
141-
return uni( () -> delegate.reactiveInsertAll( entities ) );
141+
return uni( () -> delegate.reactiveInsertAll( entities.length, entities ) );
142142
}
143143

144144
@Override
@@ -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/impl/ReactiveStatelessSessionImpl.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public class ReactiveStatelessSessionImpl extends StatelessSessionImpl implement
128128

129129
private final ReactiveConnection reactiveConnection;
130130

131-
private final ReactiveStatelessSession batchingHelperSession;
131+
private final ReactiveStatelessSessionImpl batchingHelperSession;
132132

133133
private final PersistenceContext persistenceContext;
134134

@@ -150,10 +150,9 @@ 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+
// Setting batch size to 0 because `StatelessSession` does not consider
154+
// the value of `hibernate.jdbc.batch_size`
155+
reactiveConnection = new BatchingConnection( connection, 0 );
157156
batchingHelperSession = this;
158157
influencers = new LoadQueryInfluencers( factory );
159158
}
@@ -551,9 +550,12 @@ public CompletionStage<Void> reactiveInsertAll(Object... entities) {
551550

552551
@Override
553552
public CompletionStage<Void> reactiveInsertAll(int batchSize, Object... entities) {
553+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
554+
batchingHelperSession.setJdbcBatchSize( batchSize );
554555
final ReactiveConnection connection = batchingConnection( batchSize );
555556
return loop( entities, batchingHelperSession::reactiveInsert )
556-
.thenCompose( v -> connection.executeBatch() );
557+
.thenCompose( v -> connection.executeBatch() )
558+
.whenComplete( (v, throwable) -> batchingHelperSession.setJdbcBatchSize( jdbcBatchSize ) );
557559
}
558560

559561
@Override
@@ -564,9 +566,12 @@ public CompletionStage<Void> reactiveUpdateAll(Object... entities) {
564566

565567
@Override
566568
public CompletionStage<Void> reactiveUpdateAll(int batchSize, Object... entities) {
569+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
570+
batchingHelperSession.setJdbcBatchSize( batchSize );
567571
final ReactiveConnection connection = batchingConnection( batchSize );
568572
return loop( entities, batchingHelperSession::reactiveUpdate )
569-
.thenCompose( v -> connection.executeBatch() );
573+
.thenCompose( v -> connection.executeBatch() )
574+
.whenComplete( (v, throwable) -> batchingHelperSession.setJdbcBatchSize( jdbcBatchSize ) );
570575
}
571576

572577
@Override
@@ -577,9 +582,11 @@ public CompletionStage<Void> reactiveDeleteAll(Object... entities) {
577582

578583
@Override
579584
public CompletionStage<Void> reactiveDeleteAll(int batchSize, Object... entities) {
585+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
586+
batchingHelperSession.setJdbcBatchSize( batchSize );
580587
final ReactiveConnection connection = batchingConnection( batchSize );
581-
return loop( entities, batchingHelperSession::reactiveDelete )
582-
.thenCompose( v -> connection.executeBatch() );
588+
return loop( entities, batchingHelperSession::reactiveDelete ).thenCompose( v -> connection.executeBatch() )
589+
.whenComplete( (v, throwable) -> batchingHelperSession.setJdbcBatchSize( jdbcBatchSize ) );
583590
}
584591

585592

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

592599
@Override
593600
public CompletionStage<Void> reactiveRefreshAll(int batchSize, Object... entities) {
601+
final Integer jdbcBatchSize = batchingHelperSession.getJdbcBatchSize();
602+
batchingHelperSession.setJdbcBatchSize( batchSize );
594603
final ReactiveConnection connection = batchingConnection( batchSize );
595604
return loop( entities, batchingHelperSession::reactiveRefresh )
596-
.thenCompose( v -> connection.executeBatch() );
605+
.thenCompose( v -> connection.executeBatch() )
606+
.whenComplete( (v, throwable) -> batchingHelperSession.setJdbcBatchSize( jdbcBatchSize ) );
597607
}
598608

599609
private ReactiveConnection batchingConnection(int batchSize) {

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,8 @@ default <T> CompletionStage<T> get(Class<T> entityClass, Object id, LockModeType
18361836
CompletionStage<Void> insert(Object entity);
18371837

18381838
/**
1839-
* Insert multiple rows.
1839+
* Insert multiple rows, using the number of the
1840+
* given entities as the batch size.
18401841
*
18411842
* @param entities new transient instances
18421843
*

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageStatelessSessionImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public CompletionStage<Void> insert(Object entity) {
6767

6868
@Override
6969
public CompletionStage<Void> insert(Object... entities) {
70-
return delegate.reactiveInsertAll( entities );
70+
return delegate.reactiveInsertAll( entities.length, entities );
7171
}
7272

7373
@Override

0 commit comments

Comments
 (0)