Skip to content

Commit d1d121e

Browse files
committed
provide even better documentation of the effect of JDBC batching in SS
note that this feature was most likely introduced in HHH-10431 by accident!
1 parent d28f45f commit d1d121e

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

hibernate-core/src/main/java/org/hibernate/SharedSessionContract.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ public interface SharedSessionContract extends QueryProducer, AutoCloseable, Ser
265265
* factory-level} JDBC batch size controlled by the configuration property
266266
* {@value org.hibernate.cfg.AvailableSettings#STATEMENT_BATCH_SIZE}.
267267
*
268+
* @apiNote Setting a session-level JDBC batch size for a
269+
* {@link StatelessSession} triggers a sort of write-behind behaviour
270+
* where operations are batched and executed asynchronously, undermining
271+
* the semantics of the stateless programming model. We recommend the use
272+
* of explicitly-batching operations like
273+
* {@link StatelessSession#insertMultiple insertMultiple()},
274+
* {@link StatelessSession#updateMultiple updateMultiple()}, and
275+
* {@link StatelessSession#deleteMultiple deleteMultiple()} instead.
276+
*
268277
* @param jdbcBatchSize the new session-level JDBC batch size
269278
*
270279
* @since 5.2

hibernate-core/src/main/java/org/hibernate/cfg/BatchSettings.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ public interface BatchSettings {
2121

2222
/**
2323
* Specifies the maximum number of {@linkplain java.sql.PreparedStatement statements}
24-
* to {@linkplain PreparedStatement#addBatch batch} together.
25-
* <p/>
26-
* A nonzero value enables batching
24+
* to {@linkplain PreparedStatement#addBatch batch} together in a stateful session.
25+
* <p>
26+
* Any positive value enables batching.
27+
* <p>
28+
* This setting has no effect on {@linkplain org.hibernate.StatelessSession stateless sessions}.
2729
*
2830
* @see java.sql.PreparedStatement#executeBatch
2931
* @see java.sql.PreparedStatement#addBatch

hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.hibernate.cache.spi.access.SoftLock;
2525
import org.hibernate.collection.spi.CollectionSemantics;
2626
import org.hibernate.collection.spi.PersistentCollection;
27-
import org.hibernate.engine.internal.PersistenceContexts;
2827
import org.hibernate.engine.spi.CollectionEntry;
2928
import org.hibernate.engine.spi.EffectiveEntityGraph;
3029
import org.hibernate.engine.spi.EntityHolder;
@@ -85,6 +84,7 @@
8584

8685
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
8786
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
87+
import static org.hibernate.engine.internal.PersistenceContexts.createPersistenceContext;
8888
import static org.hibernate.engine.internal.Versioning.incrementVersion;
8989
import static org.hibernate.engine.internal.Versioning.seedVersion;
9090
import static org.hibernate.engine.internal.Versioning.setVersion;
@@ -134,10 +134,12 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen
134134
public StatelessSessionImpl(SessionFactoryImpl factory, SessionCreationOptions options) {
135135
super( factory, options );
136136
connectionProvided = options.getConnection() != null;
137-
temporaryPersistenceContext = PersistenceContexts.createPersistenceContext( this );
137+
temporaryPersistenceContext = createPersistenceContext( this );
138138
influencers = new LoadQueryInfluencers( getFactory() );
139139
eventListenerGroups = factory.getEventListenerGroups();
140140
setUpMultitenancy( factory, influencers );
141+
// a nonzero batch size forces use of write-behind
142+
// therefore ignore the value of hibernate.jdbc.batch_size
141143
setJdbcBatchSize( 0 );
142144
}
143145

0 commit comments

Comments
 (0)