Skip to content

Commit bff26ec

Browse files
gliljashazzik
andauthored
Support for DbBatch (#3461)
Co-authored-by: Alex Zaytsev <[email protected]>
1 parent f214221 commit bff26ec

18 files changed

+619
-22
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ dotnet_diagnostic.NUnit2031.severity = suggestion
2727
dotnet_diagnostic.NUnit2049.severity = suggestion
2828
# The SameAs constraint always fails on value types as the actual and the expected value cannot be the same reference
2929
dotnet_diagnostic.NUnit2040.severity = suggestion
30+
dotnet_diagnostic.CA1849.severity = error
31+
dotnet_diagnostic.CA2007.severity = error
32+
dotnet_code_quality.CA2007.output_kind = DynamicallyLinkedLibrary
3033

3134
[*.xsd]
3235
indent_style = tab

src/NHibernate.Test/Ado/BatcherFixture.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44

55
namespace NHibernate.Test.Ado
66
{
7-
[TestFixture]
7+
#if NET6_0_OR_GREATER
8+
[TestFixture(true)]
9+
#endif
10+
[TestFixture(false)]
811
public class BatcherFixture: TestCase
912
{
13+
private readonly bool _useDbBatch;
14+
15+
public BatcherFixture(bool useDbBatch)
16+
{
17+
_useDbBatch = useDbBatch;
18+
}
1019
protected override string MappingsAssembly
1120
{
1221
get { return "NHibernate.Test"; }
@@ -22,10 +31,22 @@ protected override void Configure(Configuration configuration)
2231
configuration.SetProperty(Environment.FormatSql, "true");
2332
configuration.SetProperty(Environment.GenerateStatistics, "true");
2433
configuration.SetProperty(Environment.BatchSize, "10");
34+
#if NET6_0_OR_GREATER
35+
if (_useDbBatch)
36+
{
37+
configuration.SetProperty(Environment.BatchStrategy, typeof(DbBatchBatcherFactory).AssemblyQualifiedName);
38+
}
39+
#endif
2540
}
2641

2742
protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)
2843
{
44+
#if NET6_0_OR_GREATER
45+
if (_useDbBatch)
46+
{
47+
return factory.Settings.BatcherFactory is DbBatchBatcherFactory && factory.Settings.ConnectionProvider.Driver is Driver.DriverBase driverBase && driverBase.CanCreateBatch;
48+
}
49+
#endif
2950
return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory);
3051
}
3152

@@ -129,20 +150,25 @@ public void SqlClientOneRoundTripForUpdateAndInsert()
129150
Cleanup();
130151
}
131152

132-
[Test, NetFxOnly]
153+
[Test]
133154
[Description("SqlClient: The batcher log output should be formatted")]
134155
public void BatchedoutputShouldBeFormatted()
135156
{
136157
#if NETFX
137158
if (Sfi.Settings.BatcherFactory is SqlClientBatchingBatcherFactory == false)
138159
Assert.Ignore("This test is for SqlClientBatchingBatcher only");
160+
#elif NET6_0_OR_GREATER
161+
if (Sfi.Settings.BatcherFactory is DbBatchBatcherFactory == false)
162+
Assert.Ignore("This test is for DbBatchBatcherFactory only");
163+
#else
164+
Assert.Ignore("This test is for NETFX and NET6_0_OR_GREATER only");
139165
#endif
140166

141167
using (var sqlLog = new SqlLogSpy())
142168
{
143169
FillDb();
144170
var log = sqlLog.GetWholeLog();
145-
Assert.IsTrue(log.Contains("INSERT \n INTO"));
171+
Assert.That(log, Does.Contain("INSERT \n INTO").IgnoreCase);
146172
}
147173

148174
Cleanup();
@@ -213,7 +239,7 @@ public void AbstractBatcherLog()
213239
foreach (var loggingEvent in sl.Appender.GetEvents())
214240
{
215241
string message = loggingEvent.RenderedMessage;
216-
if(message.ToLowerInvariant().Contains("insert"))
242+
if(message.Contains("insert"))
217243
{
218244
Assert.That(message, Does.Contain("batch").IgnoreCase);
219245
}

src/NHibernate.Test/Async/Ado/BatcherFixture.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ namespace NHibernate.Test.Ado
1616
{
1717
using System.Threading.Tasks;
1818
using System.Threading;
19-
[TestFixture]
19+
#if NET6_0_OR_GREATER
20+
[TestFixture(true)]
21+
#endif
22+
[TestFixture(false)]
2023
public class BatcherFixtureAsync: TestCase
2124
{
25+
private readonly bool _useDbBatch;
26+
27+
public BatcherFixtureAsync(bool useDbBatch)
28+
{
29+
_useDbBatch = useDbBatch;
30+
}
2231
protected override string MappingsAssembly
2332
{
2433
get { return "NHibernate.Test"; }
@@ -34,10 +43,22 @@ protected override void Configure(Configuration configuration)
3443
configuration.SetProperty(Environment.FormatSql, "true");
3544
configuration.SetProperty(Environment.GenerateStatistics, "true");
3645
configuration.SetProperty(Environment.BatchSize, "10");
46+
#if NET6_0_OR_GREATER
47+
if (_useDbBatch)
48+
{
49+
configuration.SetProperty(Environment.BatchStrategy, typeof(DbBatchBatcherFactory).AssemblyQualifiedName);
50+
}
51+
#endif
3752
}
3853

3954
protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)
4055
{
56+
#if NET6_0_OR_GREATER
57+
if (_useDbBatch)
58+
{
59+
return factory.Settings.BatcherFactory is DbBatchBatcherFactory && factory.Settings.ConnectionProvider.Driver is Driver.DriverBase driverBase && driverBase.CanCreateBatch;
60+
}
61+
#endif
4162
return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory);
4263
}
4364

@@ -101,20 +122,25 @@ public async Task OneRoundTripUpdateAsync()
101122
await (CleanupAsync());
102123
}
103124

104-
[Test, NetFxOnly]
125+
[Test]
105126
[Description("SqlClient: The batcher log output should be formatted")]
106127
public async Task BatchedoutputShouldBeFormattedAsync()
107128
{
108129
#if NETFX
109130
if (Sfi.Settings.BatcherFactory is SqlClientBatchingBatcherFactory == false)
110131
Assert.Ignore("This test is for SqlClientBatchingBatcher only");
132+
#elif NET6_0_OR_GREATER
133+
if (Sfi.Settings.BatcherFactory is DbBatchBatcherFactory == false)
134+
Assert.Ignore("This test is for DbBatchBatcherFactory only");
135+
#else
136+
Assert.Ignore("This test is for NETFX and NET6_0_OR_GREATER only");
111137
#endif
112138

113139
using (var sqlLog = new SqlLogSpy())
114140
{
115141
await (FillDbAsync());
116142
var log = sqlLog.GetWholeLog();
117-
Assert.IsTrue(log.Contains("INSERT \n INTO"));
143+
Assert.That(log, Does.Contain("INSERT \n INTO").IgnoreCase);
118144
}
119145

120146
await (CleanupAsync());
@@ -185,7 +211,7 @@ public async Task AbstractBatcherLogAsync()
185211
foreach (var loggingEvent in sl.Appender.GetEvents())
186212
{
187213
string message = loggingEvent.RenderedMessage;
188-
if(message.ToLowerInvariant().Contains("insert"))
214+
if(message.Contains("insert"))
189215
{
190216
Assert.That(message, Does.Contain("batch").IgnoreCase);
191217
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<ItemGroup>
5757
<PackageReference Include="log4net" Version="3.0.2" />
5858
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.7.0" />
59-
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.6" />
59+
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.3" />
6060
<PackageReference Include="NHibernate.Caches.CoreDistributedCache.Memory" Version="5.9.0" />
6161
<PackageReference Include="NHibernate.Caches.Util.JsonSerializer" Version="5.9.0" />
6262
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.117" />

src/NHibernate.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1313
..\appveyor.yml = ..\appveyor.yml
1414
..\ReleaseProcedure.txt = ..\ReleaseProcedure.txt
1515
..\global.json = ..\global.json
16+
..\.editorconfig = ..\.editorconfig
1617
EndProjectSection
1718
EndProject
1819
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NHibernate", "NHibernate\NHibernate.csproj", "{5909BFE7-93CF-4E5F-BE22-6293368AF01D}"

src/NHibernate/Action/BulkOperationCleanupAction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ public virtual void Init()
166166
[Obsolete("This method has no more usage in NHibernate and will be removed in a future version.")]
167167
public virtual async Task InitAsync(CancellationToken cancellationToken)
168168
{
169-
await EvictEntityRegionsAsync(cancellationToken);
170-
await EvictCollectionRegionsAsync(cancellationToken);
169+
await EvictEntityRegionsAsync(cancellationToken).ConfigureAwait(false);
170+
await EvictCollectionRegionsAsync(cancellationToken).ConfigureAwait(false);
171171
}
172172
}
173173
}

src/NHibernate/AdoNet/ConnectionManager.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,30 @@ public void EnlistInTransaction(DbCommand command)
514514
}
515515
}
516516

517+
#if NET6_0_OR_GREATER
518+
/// <summary>
519+
/// Enlist a batch in the current transaction, if any.
520+
/// </summary>
521+
/// <param name="batch">The batch to enlist.</param>
522+
public void EnlistInTransaction(DbBatch batch)
523+
{
524+
if (batch == null)
525+
throw new ArgumentNullException(nameof(batch));
526+
527+
if (_transaction != null)
528+
{
529+
_transaction.Enlist(batch);
530+
return;
531+
}
532+
533+
if (batch.Transaction != null)
534+
{
535+
_log.Warn("set a nonnull DbBatch.Transaction to null because the Session had no Transaction");
536+
batch.Transaction = null;
537+
}
538+
}
539+
#endif
540+
517541
/// <summary>
518542
/// Enlist the connection into provided transaction if the connection should be enlisted.
519543
/// Do nothing in case an explicit transaction is ongoing.

0 commit comments

Comments
 (0)