Skip to content
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

Support for DbBatch #3461

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ dotnet_diagnostic.NUnit2031.severity = suggestion
dotnet_diagnostic.NUnit2049.severity = suggestion
# The SameAs constraint always fails on value types as the actual and the expected value cannot be the same reference
dotnet_diagnostic.NUnit2040.severity = suggestion
dotnet_diagnostic.CA1849.severity = error
dotnet_diagnostic.CA2007.severity = error
dotnet_code_quality.CA2007.output_kind = DynamicallyLinkedLibrary

[*.xsd]
indent_style = tab
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/NetCoreTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
DB_INIT: |
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P@ssw0rd" -e "MSSQL_PID=Express" -p 1433:1433 -d --name sqlexpress mcr.microsoft.com/mssql/server:2019-latest;
- DB: SqlServer2008-MicrosoftDataSqlClientDriver
CONNECTION_STRING: "Server=localhost;initial catalog=nhibernate;User Id=sa;Password=P@ssw0rd;packet size=4096;"
CONNECTION_STRING: "Server=localhost;initial catalog=nhibernate;User Id=sa;Password=P@ssw0rd;packet size=4096;TrustServerCertificate=true;"
OS: ubuntu-latest
DB_INIT: |
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P@ssw0rd" -e "MSSQL_PID=Express" -p 1433:1433 -d --name sqlexpress mcr.microsoft.com/mssql/server:2019-latest;
Expand Down
34 changes: 30 additions & 4 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,20 +150,25 @@ 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())
{
FillDb();
var log = sqlLog.GetWholeLog();
Assert.IsTrue(log.Contains("INSERT \n INTO"));
Assert.That(log, Does.Contain("INSERT \n INTO").IgnoreCase);
}

Cleanup();
Expand Down Expand Up @@ -213,7 +239,7 @@ public void AbstractBatcherLog()
foreach (var loggingEvent in sl.Appender.GetEvents())
{
string message = loggingEvent.RenderedMessage;
if(message.ToLowerInvariant().Contains("insert"))
if(message.Contains("insert"))
{
Assert.That(message, Does.Contain("batch").IgnoreCase);
}
Expand Down
34 changes: 30 additions & 4 deletions 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 && factory.Settings.ConnectionProvider.Driver is Driver.DriverBase driverBase && driverBase.CanCreateBatch;
}
#endif
return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory);
}

Expand Down Expand Up @@ -101,20 +122,25 @@ public async Task OneRoundTripUpdateAsync()
await (CleanupAsync());
}

[Test, NetFxOnly]
[Test]
[Description("SqlClient: The batcher log output should be formatted")]
public async Task BatchedoutputShouldBeFormattedAsync()
{
#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())
{
await (FillDbAsync());
var log = sqlLog.GetWholeLog();
Assert.IsTrue(log.Contains("INSERT \n INTO"));
Assert.That(log, Does.Contain("INSERT \n INTO").IgnoreCase);
}

await (CleanupAsync());
Expand Down Expand Up @@ -185,7 +211,7 @@ public async Task AbstractBatcherLogAsync()
foreach (var loggingEvent in sl.Appender.GetEvents())
{
string message = loggingEvent.RenderedMessage;
if(message.ToLowerInvariant().Contains("insert"))
if(message.Contains("insert"))
{
Assert.That(message, Does.Contain("batch").IgnoreCase);
}
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 @@ -64,7 +64,7 @@
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.17" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.7.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.1.5" />
<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
1 change: 1 addition & 0 deletions src/NHibernate.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\appveyor.yml = ..\appveyor.yml
..\ReleaseProcedure.txt = ..\ReleaseProcedure.txt
..\global.json = ..\global.json
..\.editorconfig = ..\.editorconfig
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NHibernate", "NHibernate\NHibernate.csproj", "{5909BFE7-93CF-4E5F-BE22-6293368AF01D}"
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Action/BulkOperationCleanupAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ public virtual void Init()
[Obsolete("This method has no more usage in NHibernate and will be removed in a future version.")]
public virtual async Task InitAsync(CancellationToken cancellationToken)
{
await EvictEntityRegionsAsync(cancellationToken);
await EvictCollectionRegionsAsync(cancellationToken);
await EvictEntityRegionsAsync(cancellationToken).ConfigureAwait(false);
await EvictCollectionRegionsAsync(cancellationToken).ConfigureAwait(false);
}
}
}
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 != null)
{
_transaction.Enlist(batch);
return;
}

if (batch.Transaction != null)
{
_log.Warn("set a nonnull DbBatch.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
Loading