Skip to content

Conversation

@ja-kar
Copy link

@ja-kar ja-kar commented Nov 17, 2025

Fixes #3725

Description

DbBatchBatcher throws InvalidOperationException when attempting to execute an empty batch, while
GenericBatchingBatcher and SqlClientBatchingBatcher handle this scenario correctly.

Root Cause

DbBatchBatcher.DoExecuteBatch() and DoExecuteBatchAsync() lack empty batch validation. When ExecuteBatch()
is called with an empty batch (_currentBatch.BatchCommands.Count == 0), the code attempts to execute a DbBatch
with no commands, causing:

System.InvalidOperationException: ExecuteNonQuery: CommandText property has not been initialized

Changes

Added empty batch check at the beginning of both methods, matching the pattern used in GenericBatchingBatcher:

if (_currentBatch.BatchCommands.Count == 0)
{
    Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, 0, ps);
    return;
}

Modified:

  • src/NHibernate/AdoNet/DbBatchBatcher.cs - Added guard clauses to DoExecuteBatch() and DoExecuteBatchAsync()

Added:

  • Test cases in src/NHibernate.Test/Ado/BatcherFixture.cs verifying the fix
  • Generated async test counterparts

@gliljas
Copy link
Member

gliljas commented Nov 17, 2025

Good catch and thanks for the fix!

My very personal reflection is that your tests should be included in the normal batcher tests, rather than as a NHSpecificTest.

@ja-kar
Copy link
Author

ja-kar commented Nov 17, 2025

Good point! I'll move the tests

@ja-kar ja-kar force-pushed the GH3725 branch 2 times, most recently from 40a8bce to 722f7f9 Compare November 17, 2025 15:11
Add empty batch validation to DoExecuteBatch() and DoExecuteBatchAsync()
to prevent InvalidOperationException when ExecuteBatch is called with no
commands. This matches the pattern used in GenericBatchingBatcher.
@ja-kar ja-kar marked this pull request as ready for review November 17, 2025 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DbBatchBatcher throws InvalidOperationException on empty batch execution

2 participants