StatelessSession#insertMultiple batch size not applied

From reading the Javadoc of StatelessSession and the source of StatelessSessionImpl#insertMultiple it is our understanding that if JDBC batching is globally enabled then #insertMultiple should use the size of the List parameter as the batch size, rather than the global batch size. We are observing the opposite.

Here is what we are observing in detail:

We are on 7.4.14.Final and using a global batch size of 10 (hibernate.jdbc.batch_size=10). We are inserting 1000 entities using 10 calls to #insertMultiple, each with a list size of 100. Looking a the source of #insertMultiple we would expect 10 batch inserts, each with a batch size 100. However looking at flight recordings and JDBC trace logging we see that instead 100 batch inserts with a batch size of 10 are performed.

We managed to track the issue down to StandardMutationExecutorService#createExecutor. Rather than reading the JDBC batch size from SharedSessionContractImplementor it instead calls out to session.getJdbcCoordinator().getJdbcSessionOwner().getJdbcBatchSize(). We have a simple fix. However we are struggling with two issues:

  1. We are unsure about the wider implications of the fix. ./gradlew build runs but that does not help assessing the impact.
  2. We are struggling to come up with a test. We are aware of hibernate-test-case-templates but couldn’t find anything that helps counting batch inserts. We didn’t find any direct tests for StandardMutationExecutorService, we could come up with one relaying heavily on mocking.

You can register a BatchObserver and count the number of invocations to assert in the test.

We’ll give it a try.

We were able to create a reproducer. The issue happens when a StatelessSession is created from a Session using

session.statelessWithOptions().connection().open()

in this case the session owner is the Session but the batch size is stored in StatelessSession. This is how Spring creates a StatelessSession, so it may be a common issue.

If you agree with the assessment and the fix we’d go forward, create a JIRA and submit a PR.

Yes, sounds like a bug. Please create a Jira and PR.

There you are HHH-21089 Read batch size from session by marschall · Pull Request #13875 · hibernate/hibernate-orm · GitHub. Astra seems to have found a related bug when it comes to write ordering of pending updates.