Skip to content

Commit

Permalink
Add support for DbBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
gliljas committed Mar 14, 2024
1 parent 1a960b0 commit 541fefb
Show file tree
Hide file tree
Showing 17 changed files with 611 additions and 12 deletions.
30 changes: 28 additions & 2 deletions src/NHibernate.Test/Ado/BatcherFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@

namespace NHibernate.Test.Ado
{
[TestFixture]
#if NET6_0_OR_GREATER
[TestFixture(true)]
#endif
[TestFixture(false)]
public class BatcherFixture: TestCase
{
private readonly bool _useDbBatch;

public BatcherFixture(bool useDbBatch)
{
_useDbBatch = useDbBatch;
}
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
Expand All @@ -22,10 +31,22 @@ protected override void Configure(Configuration configuration)
configuration.SetProperty(Environment.FormatSql, "true");
configuration.SetProperty(Environment.GenerateStatistics, "true");
configuration.SetProperty(Environment.BatchSize, "10");
#if NET6_0_OR_GREATER
if (_useDbBatch)
{
configuration.SetProperty(Environment.BatchStrategy, typeof(DbBatchBatcherFactory).AssemblyQualifiedName);
}
#endif
}

protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)
{
#if NET6_0_OR_GREATER
if (_useDbBatch)
{
return factory.Settings.BatcherFactory is DbBatchBatcherFactory && factory.Settings.ConnectionProvider.Driver is Driver.DriverBase driverBase && driverBase.CanCreateBatch;
}
#endif
return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory);
}

Expand Down Expand Up @@ -129,13 +150,18 @@ public void SqlClientOneRoundTripForUpdateAndInsert()
Cleanup();
}

[Test, NetFxOnly]
[Test]
[Description("SqlClient: The batcher log output should be formatted")]
public void BatchedoutputShouldBeFormatted()
{
#if NETFX
if (Sfi.Settings.BatcherFactory is SqlClientBatchingBatcherFactory == false)
Assert.Ignore("This test is for SqlClientBatchingBatcher only");
#elif NET6_0_OR_GREATER
if (Sfi.Settings.BatcherFactory is DbBatchBatcherFactory == false)
Assert.Ignore("This test is for DbBatchBatcherFactory only");
#else
Assert.Ignore("This test is for NETFX and NET6_0_OR_GREATER only");
#endif

using (var sqlLog = new SqlLogSpy())
Expand Down
23 changes: 22 additions & 1 deletion src/NHibernate.Test/Async/Ado/BatcherFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ namespace NHibernate.Test.Ado
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
#if NET6_0_OR_GREATER
[TestFixture(true)]
#endif
[TestFixture(false)]
public class BatcherFixtureAsync: TestCase
{
private readonly bool _useDbBatch;

public BatcherFixtureAsync(bool useDbBatch)
{
_useDbBatch = useDbBatch;
}
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
Expand All @@ -34,10 +43,22 @@ protected override void Configure(Configuration configuration)
configuration.SetProperty(Environment.FormatSql, "true");
configuration.SetProperty(Environment.GenerateStatistics, "true");
configuration.SetProperty(Environment.BatchSize, "10");
#if NET6_0_OR_GREATER
if (_useDbBatch)
{
configuration.SetProperty(Environment.BatchStrategy, typeof(DbBatchBatcherFactory).AssemblyQualifiedName);
}
#endif
}

protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)
{
#if NET6_0_OR_GREATER
if (_useDbBatch)
{
return factory.Settings.BatcherFactory is DbBatchBatcherFactory;
}
#endif
return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory);
}

Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.7.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.1.3" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" />
<PackageReference Include="NHibernate.Caches.CoreDistributedCache.Memory" Version="5.9.0" />
<PackageReference Include="NHibernate.Caches.Util.JsonSerializer" Version="5.9.0" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.117" />
Expand Down
24 changes: 24 additions & 0 deletions src/NHibernate/AdoNet/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,30 @@ public void EnlistInTransaction(DbCommand command)
}
}

#if NET6_0_OR_GREATER
/// <summary>
/// Enlist a batch in the current transaction, if any.
/// </summary>
/// <param name="batch">The batch to enlist.</param>
public void EnlistInTransaction(DbBatch batch)
{
if (batch == null)
throw new ArgumentNullException(nameof(batch));

if (_transaction is ITransactionWithBatchSupport transactionWithBatch)
{
transactionWithBatch.Enlist(batch);
return;
}

if (batch.Transaction != null)
{
_log.Warn("set a nonnull DbCommand.Transaction to null because the Session had no Transaction");
batch.Transaction = null;
}
}
#endif

/// <summary>
/// Enlist the connection into provided transaction if the connection should be enlisted.
/// Do nothing in case an explicit transaction is ongoing.
Expand Down
Loading

0 comments on commit 541fefb

Please sign in to comment.