Skip to content

Add an overridable ADO BeginTransaction method #1910

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

Merged
merged 3 commits into from
Nov 26, 2018
Merged
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
1 change: 1 addition & 0 deletions src/NHibernate/Async/Transaction/AdoTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using NHibernate.Driver;
using NHibernate.Engine;
using NHibernate.Impl;

Expand Down
15 changes: 15 additions & 0 deletions src/NHibernate/Driver/DriverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ protected bool IsPrepareSqlEnabled
public abstract DbConnection CreateConnection();
public abstract DbCommand CreateCommand();

/// <summary>
/// Begin an ADO <see cref="DbTransaction" />.
/// </summary>
/// <param name="isolationLevel">The isolation level requested for the transaction.</param>
/// <param name="connection">The connection on which to start the transaction.</param>
/// <returns>The started <see cref="DbTransaction" />.</returns>
public virtual DbTransaction BeginTransaction(IsolationLevel isolationLevel, DbConnection connection)
{
if (isolationLevel == IsolationLevel.Unspecified)
{
return connection.BeginTransaction();
}
return connection.BeginTransaction(isolationLevel);
}

/// <summary>
/// Does this Driver require the use of a Named Prefix in the SQL statement.
/// </summary>
Expand Down
28 changes: 26 additions & 2 deletions src/NHibernate/Driver/DriverExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
using System.Data;
using System.Data.Common;
using NHibernate.AdoNet;
using NHibernate.SqlTypes;
using NHibernate.Type;

namespace NHibernate.Driver
{
internal static class DriverExtensions
public static class DriverExtensions
{
internal static void AdjustParameterForValue(this IDriver driver, DbParameter parameter, SqlType sqlType, object value)
{
var adjustingDriver = driver as IParameterAdjuster;
adjustingDriver?.AdjustParameterForValue(parameter, sqlType, value);
}

// 6.0 TODO: merge into IDriver
/// <summary>
/// Begin an ADO <see cref="DbTransaction" />.
/// </summary>
/// <param name="driver">The driver.</param>
/// <param name="isolationLevel">The isolation level requested for the transaction.</param>
/// <param name="connection">The connection on which to start the transaction.</param>
/// <returns>The started <see cref="DbTransaction" />.</returns>
public static DbTransaction BeginTransaction(this IDriver driver, IsolationLevel isolationLevel, DbConnection connection)
{
if (driver is DriverBase driverBase)
{
return driverBase.BeginTransaction(isolationLevel, connection);
}

// So long for custom drivers not deriving from DriverBase, they will have to wait for 6.0 if they
// need the feature.
if (isolationLevel == IsolationLevel.Unspecified)
{
return connection.BeginTransaction();
}
return connection.BeginTransaction(isolationLevel);
}
}
}
10 changes: 2 additions & 8 deletions src/NHibernate/Transaction/AdoTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using NHibernate.Driver;
using NHibernate.Engine;
using NHibernate.Impl;

Expand Down Expand Up @@ -146,14 +147,7 @@ public void Begin(IsolationLevel isolationLevel)

try
{
if (isolationLevel == IsolationLevel.Unspecified)
{
trans = session.Connection.BeginTransaction();
}
else
{
trans = session.Connection.BeginTransaction(isolationLevel);
}
trans = session.Factory.ConnectionProvider.Driver.BeginTransaction(isolationLevel, session.Connection);
}
catch (HibernateException)
{
Expand Down