Skip to content

Commit 41119ba

Browse files
dreab8DavideD
authored andcommitted
[#2108] StatelessSession insertAll in batch does not do batching
1 parent f4bd7ad commit 41119ba

File tree

5 files changed

+45
-27
lines changed

5 files changed

+45
-27
lines changed

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

+8-4
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
*
@@ -1817,7 +1818,8 @@ default <T> Uni<T> get(Class<T> entityClass, Object id, LockModeType lockModeTyp
18171818
Uni<Void> delete(Object entity);
18181819

18191820
/**
1820-
* Delete multiple rows.
1821+
* Delete multiple rows, using the number of the
1822+
* given entities as the batch size.
18211823
*
18221824
* @param entities detached entity instances
18231825
*
@@ -1855,7 +1857,8 @@ default <T> Uni<T> get(Class<T> entityClass, Object id, LockModeType lockModeTyp
18551857
Uni<Void> update(Object entity);
18561858

18571859
/**
1858-
* Update multiple rows.
1860+
* Update multiple rows, using the number of the
1861+
* given entities as the batch size.
18591862
*
18601863
* @param entities detached entity instances
18611864
*
@@ -1915,7 +1918,8 @@ default <T> Uni<T> get(Class<T> entityClass, Object id, LockModeType lockModeTyp
19151918
Uni<Void> refresh(Object entity);
19161919

19171920
/**
1918-
* Refresh the entity instance state from the database.
1921+
* Refresh the entity instance state from the database, using the number of the
1922+
* given entities as the batch size.
19191923
*
19201924
* @param entities The entities to be refreshed.
19211925
*

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

+5-5
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
@@ -158,12 +158,12 @@ public Uni<Void> delete(Object entity) {
158158

159159
@Override
160160
public Uni<Void> deleteAll(Object... entities) {
161-
return uni( () -> delegate.reactiveDeleteAll( entities ) );
161+
return uni( () -> delegate.reactiveDeleteAll( entities.length, entities ) );
162162
}
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
@@ -178,7 +178,7 @@ public Uni<Void> update(Object entity) {
178178

179179
@Override
180180
public Uni<Void> updateAll(Object... entities) {
181-
return uni( () -> delegate.reactiveUpdateAll( entities ) );
181+
return uni( () -> delegate.reactiveUpdateAll( entities.length, entities ) );
182182
}
183183

184184
@Override
@@ -208,7 +208,7 @@ public Uni<Void> upsert(String entityName, Object entity) {
208208

209209
@Override
210210
public Uni<Void> refreshAll(Object... entities) {
211-
return uni( () -> delegate.reactiveRefreshAll( entities ) );
211+
return uni( () -> delegate.reactiveRefreshAll( entities.length, entities ) );
212212
}
213213

214214
@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

+8-4
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
*
@@ -1874,7 +1875,8 @@ default <T> CompletionStage<T> get(Class<T> entityClass, Object id, LockModeType
18741875
CompletionStage<Void> delete(Object entity);
18751876

18761877
/**
1877-
* Delete multiple rows.
1878+
* Delete multiple rows, using the number of the
1879+
* given entities as the batch size.
18781880
*
18791881
* @param entities detached entity instances
18801882
*
@@ -1912,7 +1914,8 @@ default <T> CompletionStage<T> get(Class<T> entityClass, Object id, LockModeType
19121914
CompletionStage<Void> update(Object entity);
19131915

19141916
/**
1915-
* Update multiple rows.
1917+
* Update multiple rows, using the number of the
1918+
* given entities as the batch size.
19161919
*
19171920
* @param entities a detached entity instance
19181921
*
@@ -1950,7 +1953,8 @@ default <T> CompletionStage<T> get(Class<T> entityClass, Object id, LockModeType
19501953
CompletionStage<Void> refresh(Object entity);
19511954

19521955
/**
1953-
* Refresh the entity instance state from the database.
1956+
* Refresh the entity instance state from the database, using the number of the
1957+
* given entities as the batch size.
19541958
*
19551959
* @param entities The entities to be refreshed.
19561960
*

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

+4-4
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
@@ -87,7 +87,7 @@ public CompletionStage<Void> delete(Object entity) {
8787

8888
@Override
8989
public CompletionStage<Void> delete(Object... entities) {
90-
return delegate.reactiveDeleteAll( entities );
90+
return delegate.reactiveDeleteAll( entities.length, entities );
9191
}
9292

9393
@Override
@@ -107,7 +107,7 @@ public CompletionStage<Void> update(Object entity) {
107107

108108
@Override
109109
public CompletionStage<Void> update(Object... entities) {
110-
return delegate.reactiveUpdateAll( entities );
110+
return delegate.reactiveUpdateAll( entities.length, entities );
111111
}
112112

113113
@Override
@@ -127,7 +127,7 @@ public CompletionStage<Void> refresh(Object entity) {
127127

128128
@Override
129129
public CompletionStage<Void> refresh(Object... entities) {
130-
return delegate.reactiveRefreshAll( entities );
130+
return delegate.reactiveRefreshAll( entities.length, entities );
131131
}
132132

133133
@Override

0 commit comments

Comments
 (0)