Skip to content

NH-4013 - SQL-Server batcher failure after CloseCommands. #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/NHibernate.Test/ConnectionTest/BatcherFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Cfg;
using NHibernate.Util;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.ConnectionTest
{
[TestFixture]
public class BatcherFixture : ConnectionManagementTestCase
{
protected override void Configure(Configuration config)
{
base.Configure(config);
config.SetProperty(Environment.BatchSize, "10");
}

protected override ISession GetSessionUnderTest()
=> OpenSession();

protected override void OnTearDown()
{
using (var s = OpenSession())
{
s.CreateQuery("delete from System.Object").ExecuteUpdate();
}
}

[Test]
public void CanCloseCommandsAndUseBatcher()
{
using (var s = OpenSession())
{
// Need a generator strategy not causing insert at save.
var silly = new YetAnother { Name = "Silly" };
s.Save(silly);
s.GetSessionImplementation().ConnectionManager.Batcher.CloseCommands();

Assert.DoesNotThrow(s.Flush, "Flush failure after closing commands.");
}
}
}
}
7 changes: 7 additions & 0 deletions src/NHibernate.Test/ConnectionTest/Silly.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@
</id>
<property name="Name"/>
</class>

<class name="YetAnother">
<id name="Id" type="long">
<generator class="hilo"/>
</id>
<property name="Name"/>
</class>
</hibernate-mapping>
12 changes: 12 additions & 0 deletions src/NHibernate.Test/ConnectionTest/YetAnother.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace NHibernate.Test.ConnectionTest
{
[Serializable]
public class YetAnother
{
public virtual long Id { get; set; }

public virtual string Name { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@
<Compile Include="CompositeId\Order.cs" />
<Compile Include="CompositeId\Product.cs" />
<Compile Include="ConnectionStringTest\NamedConnectionStringFixture.cs" />
<Compile Include="ConnectionTest\BatcherFixture.cs" />
<Compile Include="ConnectionTest\AggressiveReleaseTest.cs" />
<Compile Include="ConnectionTest\ConnectionManagementTestCase.cs" />
<Compile Include="ConnectionTest\YetAnother.cs" />
<Compile Include="ConnectionTest\Other.cs" />
<Compile Include="ConnectionTest\Silly.cs" />
<Compile Include="ConnectionTest\MapBasedSessionContextFixture.cs" />
Expand Down
67 changes: 48 additions & 19 deletions src/NHibernate/AdoNet/MySqlClientBatchingBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,48 @@ public override void AddToBatch(IExpectation expectation)

protected override void DoExecuteBatch(DbCommand ps)
{
Log.DebugFormat("Executing batch");
CheckReaders();
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
{
Factory.Settings.SqlStatementLogger.LogBatchCommand(currentBatchCommandsLog.ToString());
currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
}

int rowsAffected;
try
{
rowsAffected = currentBatch.ExecuteNonQuery();
Log.DebugFormat("Executing batch");
CheckReaders();
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
{
Factory.Settings.SqlStatementLogger.LogBatchCommand(currentBatchCommandsLog.ToString());
}

int rowsAffected;
try
{
rowsAffected = currentBatch.ExecuteNonQuery();
}
catch (DbException e)
{
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
}

Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected);
}
catch (DbException e)
finally
{
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
ClearCurrentBatch();
}
}

Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected);
private MySqlClientSqlCommandSet CreateConfiguredBatch()
{
return new MySqlClientSqlCommandSet(batchSize);
}

private void ClearCurrentBatch()
{
currentBatch.Dispose();
totalExpectedRowsAffected = 0;
currentBatch = CreateConfiguredBatch();
}

private MySqlClientSqlCommandSet CreateConfiguredBatch()
{
return new MySqlClientSqlCommandSet(batchSize);
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
{
currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
}
}

public override void CloseCommands()
Expand All @@ -105,12 +119,27 @@ public override void CloseCommands()

try
{
currentBatch.Dispose();
ClearCurrentBatch();
}
catch (Exception e)
{
// Prevent exceptions when closing the batch from hiding any original exception
// Prevent exceptions when clearing the batch from hiding any original exception
// (We do not know here if this batch closing occurs after a failure or not.)
Log.Warn("Exception clearing batch", e);
}
}

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
// Prevent exceptions when closing the batch from hiding any original exception
// (We do not know here if this batch closing occurs after a failure or not.)
try
{
currentBatch.Dispose();
}
catch (Exception e)
{
Log.Warn("Exception closing batcher", e);
}
}
Expand Down
72 changes: 50 additions & 22 deletions src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void AddToBatch(IExpectation expectation)
{
Log.Debug("Adding to batch:" + lineWithParameters);
}
_currentBatch.Append((System.Data.SqlClient.SqlCommand) batchUpdate);
_currentBatch.Append((System.Data.SqlClient.SqlCommand)batchUpdate);

if (_currentBatch.CountOfCommands >= _batchSize)
{
Expand All @@ -71,30 +71,31 @@ public override void AddToBatch(IExpectation expectation)

protected override void DoExecuteBatch(DbCommand ps)
{
Log.DebugFormat("Executing batch");
CheckReaders();
Prepare(_currentBatch.BatchCommand);
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
{
Factory.Settings.SqlStatementLogger.LogBatchCommand(_currentBatchCommandsLog.ToString());
_currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
}

int rowsAffected;
try
{
rowsAffected = _currentBatch.ExecuteNonQuery();
Log.DebugFormat("Executing batch");
CheckReaders();
Prepare(_currentBatch.BatchCommand);
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
{
Factory.Settings.SqlStatementLogger.LogBatchCommand(_currentBatchCommandsLog.ToString());
}
int rowsAffected;
try
{
rowsAffected = _currentBatch.ExecuteNonQuery();
}
catch (DbException e)
{
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
}

Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected);
}
catch (DbException e)
finally
{
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
ClearCurrentBatch();
}

Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected);

_currentBatch.Dispose();
_totalExpectedRowsAffected = 0;
_currentBatch = CreateConfiguredBatch();
}

private SqlClientSqlCommandSet CreateConfiguredBatch()
Expand All @@ -118,18 +119,45 @@ private SqlClientSqlCommandSet CreateConfiguredBatch()
return result;
}

private void ClearCurrentBatch()
{
_currentBatch.Dispose();
_totalExpectedRowsAffected = 0;
_currentBatch = CreateConfiguredBatch();

if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
{
_currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
}
}

public override void CloseCommands()
{
base.CloseCommands();

// Prevent exceptions when closing the batch from hiding any original exception
// (We do not know here if this batch closing occurs after a failure or not.)
try
{
ClearCurrentBatch();
}
catch (Exception e)
{
Log.Warn("Exception clearing batch", e);
}
}

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
// Prevent exceptions when closing the batch from hiding any original exception
// (We do not know here if this batch closing occurs after a failure or not.)
try
{
_currentBatch.Dispose();
}
catch (Exception e)
{
// Prevent exceptions when closing the batch from hiding any original exception
// (We do not know here if this batch closing occurs after a failure or not.)
Log.Warn("Exception closing batcher", e);
}
}
Expand Down