Skip to content

Commit 0c3c49b

Browse files
Add an overridable ADO BeginTransaction method (#1910)
Fixes #1908 Co-authored-by: Andreas Fischer <greenlight2k@yahoo.de>
1 parent 9bd9a80 commit 0c3c49b

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

src/NHibernate/Async/Transaction/AdoTransaction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Collections.Generic;
1313
using System.Data;
1414
using System.Data.Common;
15+
using NHibernate.Driver;
1516
using NHibernate.Engine;
1617
using NHibernate.Impl;
1718

src/NHibernate/Driver/DriverBase.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ protected bool IsPrepareSqlEnabled
4646
public abstract DbConnection CreateConnection();
4747
public abstract DbCommand CreateCommand();
4848

49+
/// <summary>
50+
/// Begin an ADO <see cref="DbTransaction" />.
51+
/// </summary>
52+
/// <param name="isolationLevel">The isolation level requested for the transaction.</param>
53+
/// <param name="connection">The connection on which to start the transaction.</param>
54+
/// <returns>The started <see cref="DbTransaction" />.</returns>
55+
public virtual DbTransaction BeginTransaction(IsolationLevel isolationLevel, DbConnection connection)
56+
{
57+
if (isolationLevel == IsolationLevel.Unspecified)
58+
{
59+
return connection.BeginTransaction();
60+
}
61+
return connection.BeginTransaction(isolationLevel);
62+
}
63+
4964
/// <summary>
5065
/// Does this Driver require the use of a Named Prefix in the SQL statement.
5166
/// </summary>
Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1+
using System.Data;
12
using System.Data.Common;
23
using NHibernate.AdoNet;
34
using NHibernate.SqlTypes;
4-
using NHibernate.Type;
55

66
namespace NHibernate.Driver
77
{
8-
internal static class DriverExtensions
8+
public static class DriverExtensions
99
{
1010
internal static void AdjustParameterForValue(this IDriver driver, DbParameter parameter, SqlType sqlType, object value)
1111
{
1212
var adjustingDriver = driver as IParameterAdjuster;
1313
adjustingDriver?.AdjustParameterForValue(parameter, sqlType, value);
1414
}
15+
16+
// 6.0 TODO: merge into IDriver
17+
/// <summary>
18+
/// Begin an ADO <see cref="DbTransaction" />.
19+
/// </summary>
20+
/// <param name="driver">The driver.</param>
21+
/// <param name="isolationLevel">The isolation level requested for the transaction.</param>
22+
/// <param name="connection">The connection on which to start the transaction.</param>
23+
/// <returns>The started <see cref="DbTransaction" />.</returns>
24+
public static DbTransaction BeginTransaction(this IDriver driver, IsolationLevel isolationLevel, DbConnection connection)
25+
{
26+
if (driver is DriverBase driverBase)
27+
{
28+
return driverBase.BeginTransaction(isolationLevel, connection);
29+
}
30+
31+
// So long for custom drivers not deriving from DriverBase, they will have to wait for 6.0 if they
32+
// need the feature.
33+
if (isolationLevel == IsolationLevel.Unspecified)
34+
{
35+
return connection.BeginTransaction();
36+
}
37+
return connection.BeginTransaction(isolationLevel);
38+
}
1539
}
1640
}

src/NHibernate/Transaction/AdoTransaction.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Data.Common;
5+
using NHibernate.Driver;
56
using NHibernate.Engine;
67
using NHibernate.Impl;
78

@@ -146,14 +147,7 @@ public void Begin(IsolationLevel isolationLevel)
146147

147148
try
148149
{
149-
if (isolationLevel == IsolationLevel.Unspecified)
150-
{
151-
trans = session.Connection.BeginTransaction();
152-
}
153-
else
154-
{
155-
trans = session.Connection.BeginTransaction(isolationLevel);
156-
}
150+
trans = session.Factory.ConnectionProvider.Driver.BeginTransaction(isolationLevel, session.Connection);
157151
}
158152
catch (HibernateException)
159153
{

0 commit comments

Comments
 (0)