Skip to content

Commit c6ea4a2

Browse files
committed
Move Prepare(DbBatch) to DbBatchBatcher
1 parent 745484d commit c6ea4a2

File tree

6 files changed

+47
-52
lines changed

6 files changed

+47
-52
lines changed

src/NHibernate.Test/Ado/BatcherFixture.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ protected override void Configure(Configuration configuration)
3131
configuration.SetProperty(Environment.FormatSql, "true");
3232
configuration.SetProperty(Environment.GenerateStatistics, "true");
3333
configuration.SetProperty(Environment.BatchSize, "10");
34+
#if NET6_0_OR_GREATER
3435
if (_useDbBatch)
3536
{
36-
configuration.SetProperty("usedbbatch", "true");
37+
configuration.SetProperty(Environment.BatchStrategy, typeof(DbBatchBatcherFactory).AssemblyQualifiedName);
3738
}
39+
#endif
3840
}
3941

4042
protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ protected override void Configure(Configuration configuration)
4343
configuration.SetProperty(Environment.FormatSql, "true");
4444
configuration.SetProperty(Environment.GenerateStatistics, "true");
4545
configuration.SetProperty(Environment.BatchSize, "10");
46+
#if NET6_0_OR_GREATER
4647
if (_useDbBatch)
4748
{
48-
configuration.SetProperty("usedbbatch", "true");
49+
configuration.SetProperty(Environment.BatchStrategy, typeof(DbBatchBatcherFactory).AssemblyQualifiedName);
4950
}
51+
#endif
5052
}
5153

5254
protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)

src/NHibernate/AdoNet/AbstractBatcher.cs

-39
Original file line numberDiff line numberDiff line change
@@ -136,45 +136,6 @@ protected void Prepare(DbCommand cmd)
136136
}
137137
}
138138

139-
#if NET6_0_OR_GREATER
140-
/// <summary>
141-
/// Prepares the <see cref="DbBatch"/> for execution in the database.
142-
/// </summary>
143-
/// <remarks>
144-
/// This takes care of hooking the <see cref="DbBatch"/> up to an <see cref="DbConnection"/>
145-
/// and <see cref="DbTransaction"/> if one exists. It will call <c>Prepare</c> if the Driver
146-
/// supports preparing batches.
147-
/// </remarks>
148-
protected void Prepare(DbBatch batch)
149-
{
150-
try
151-
{
152-
var sessionConnection = _connectionManager.GetConnection();
153-
154-
if (batch.Connection != null)
155-
{
156-
// make sure the commands connection is the same as the Sessions connection
157-
// these can be different when the session is disconnected and then reconnected
158-
if (batch.Connection != sessionConnection)
159-
{
160-
batch.Connection = sessionConnection;
161-
}
162-
}
163-
else
164-
{
165-
batch.Connection = sessionConnection;
166-
}
167-
168-
_connectionManager.EnlistInTransaction(batch);
169-
(Driver as IDriverWithBatchSupport).PrepareBatch(batch);
170-
}
171-
catch (InvalidOperationException ioe)
172-
{
173-
throw new ADOException("While preparing " + string.Join(Environment.NewLine, batch.BatchCommands.Select(x=>x.CommandText)) + " an error occurred", ioe);
174-
}
175-
}
176-
#endif
177-
178139
public virtual DbCommand PrepareBatchCommand(CommandType type, SqlString sql, SqlType[] parameterTypes)
179140
{
180141
if (sql.Equals(_batchCommandSql) && ArrayHelper.ArrayEquals(parameterTypes, _batchCommandParameterTypes))

src/NHibernate/AdoNet/DbBatchBatcher.cs

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#if NET6_0_OR_GREATER
22
using System;
33
using System.Data.Common;
4+
using System.Linq;
45
using System.Text;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using NHibernate.AdoNet.Util;
89
using NHibernate.Driver;
910
using NHibernate.Engine;
1011
using NHibernate.Exceptions;
11-
using NHibernate.Util;
1212

1313
namespace NHibernate.AdoNet
1414
{
@@ -19,6 +19,7 @@ public class DbBatchBatcher : AbstractBatcher
1919
private DbBatch _currentBatch;
2020
private StringBuilder _currentBatchCommandsLog;
2121
private readonly int _defaultTimeout;
22+
private readonly ConnectionManager _connectionManager;
2223

2324
public DbBatchBatcher(ConnectionManager connectionManager, IInterceptor interceptor)
2425
: base(connectionManager, interceptor)
@@ -32,6 +33,7 @@ public DbBatchBatcher(ConnectionManager connectionManager, IInterceptor intercep
3233
//behind an if(log.IsDebugEnabled) will cause a null reference exception
3334
//at that point.
3435
_currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
36+
_connectionManager = connectionManager;
3537
}
3638

3739
public override int BatchSize
@@ -253,6 +255,43 @@ protected override void Dispose(bool isDisposing)
253255
Log.Warn(e, "Exception closing batcher");
254256
}
255257
}
258+
259+
/// <summary>
260+
/// Prepares the <see cref="DbBatch"/> for execution in the database.
261+
/// </summary>
262+
/// <remarks>
263+
/// This takes care of hooking the <see cref="DbBatch"/> up to an <see cref="DbConnection"/>
264+
/// and <see cref="DbTransaction"/> if one exists. It will call <c>Prepare</c> if the Driver
265+
/// supports preparing batches.
266+
/// </remarks>
267+
protected void Prepare(DbBatch batch)
268+
{
269+
try
270+
{
271+
var sessionConnection = _connectionManager.GetConnection();
272+
273+
if (batch.Connection != null)
274+
{
275+
// make sure the commands connection is the same as the Sessions connection
276+
// these can be different when the session is disconnected and then reconnected
277+
if (batch.Connection != sessionConnection)
278+
{
279+
batch.Connection = sessionConnection;
280+
}
281+
}
282+
else
283+
{
284+
batch.Connection = sessionConnection;
285+
}
286+
287+
_connectionManager.EnlistInTransaction(batch);
288+
(Driver as IDriverWithBatchSupport).PrepareBatch(batch);
289+
}
290+
catch (InvalidOperationException ioe)
291+
{
292+
throw new ADOException("While preparing " + string.Join(Environment.NewLine, batch.BatchCommands.Select(x => x.CommandText)) + " an error occurred", ioe);
293+
}
294+
}
256295
}
257296

258297
public class DbBatchBatcherFactory : IBatcherFactory

src/NHibernate/Async/AdoNet/AbstractBatcher.cs

-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ protected async Task PrepareAsync(DbCommand cmd, CancellationToken cancellationT
6868
}
6969
}
7070

71-
#if NET6_0_OR_GREATER
72-
#endif
73-
7471
public virtual async Task<DbCommand> PrepareBatchCommandAsync(CommandType type, SqlString sql, SqlType[] parameterTypes, CancellationToken cancellationToken)
7572
{
7673
cancellationToken.ThrowIfCancellationRequested();

src/NHibernate/Driver/MicrosoftDataSqlClientDriver.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class MicrosoftDataSqlClientDriver : ReflectionBasedDriver, IEmbeddedBatc
2020
private static readonly Action<object, SqlDbType> SetSqlDbType = DelegateHelper.BuildPropertySetter<SqlDbType>(System.Type.GetType("Microsoft.Data.SqlClient.SqlParameter, Microsoft.Data.SqlClient", true), "SqlDbType");
2121

2222
private Dialect.Dialect _dialect;
23-
private bool _useDbBatch;
2423

2524
public MicrosoftDataSqlClientDriver()
2625
: base(
@@ -71,11 +70,8 @@ public MicrosoftDataSqlClientDriver()
7170
/// <inheritdoc />
7271
public override DateTime MinDate => DateTime.MinValue;
7372

74-
#if NET6_0_OR_GREATER
75-
System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass => _useDbBatch && CanCreateBatch ? typeof(DbBatchBatcherFactory) : typeof(GenericBatchingBatcherFactory);
76-
#else
73+
7774
System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass => typeof(GenericBatchingBatcherFactory);
78-
#endif
7975

8076
/// <inheritdoc />
8177
public virtual void AdjustParameterForValue(DbParameter parameter, SqlType sqlType, object value)
@@ -103,8 +99,6 @@ public override void Configure(IDictionary<string, string> settings)
10399
base.Configure(settings);
104100

105101
_dialect = Dialect.Dialect.GetDialect(settings);
106-
107-
_useDbBatch = settings.TryGetValue("usedbbatch", out var value) && bool.Parse(value);
108102
}
109103

110104
/// <inheritdoc />

0 commit comments

Comments
 (0)