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
@fredericDelaporte
Copy link
Member

I am trying the test without the fix, and I do not get a failure. The test must demonstrate the trouble.

Test run with PostgreSQL, after having reset mixed the commit, and undone the change on NHibernate to only keep the change on NHibernate.Test.
image

@ja-kar
Copy link
Author

ja-kar commented Dec 1, 2025

I have updated the test to be more like the scenario I encountered the bug in and verified that the tests now fail without the fix on SQL Server

@fredericDelaporte
Copy link
Member

fredericDelaporte commented Dec 7, 2025

I have tried with your test update and SQL Server: under net8, the test is ignored. Under net4.8, there is still no failure without your fix. With PostgreSQL, still no failures either.

So, I am now launching the whole CI on your test to check if anything starts failing, see #3733.

@fredericDelaporte fredericDelaporte added this to the next minor milestone Dec 7, 2025
@ja-kar
Copy link
Author

ja-kar commented Dec 8, 2025

Did you use NHibernate.Driver.MicrosoftDataSqlClientDriver as the driver on .NET8? Wasn't ignored for me. On net4.8 it wont fail though as this bug only exists in the DbBatchBatcher afaik. Maybe it should be locked down to only run for .NET8 then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DbBatchBatcher may fail on empty batch execution

3 participants