Skip to content

Commit dbeda66

Browse files
Fix some NRE in work isolation and connection handling
Fixes #2336
1 parent 6c252ee commit dbeda66

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

src/NHibernate/Async/Transaction/AdoNetTransactionFactory.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ async Task InternalExecuteWorkInIsolationAsync()
4747
{
4848
// We make an exception for SQLite and use the session's connection,
4949
// since SQLite only allows one connection to the database.
50-
if (session.Factory.Dialect is SQLiteDialect)
51-
connection = session.Connection;
52-
else
53-
connection = await (session.Factory.ConnectionProvider.GetConnectionAsync(cancellationToken)).ConfigureAwait(false);
50+
var connection = session.Factory.Dialect is SQLiteDialect
51+
? session.Connection
52+
: await (session.Factory.ConnectionProvider.GetConnectionAsync(cancellationToken)).ConfigureAwait(false);
5453

5554
if (transacted)
5655
{
@@ -126,7 +125,7 @@ async Task InternalExecuteWorkInIsolationAsync()
126125
isolaterLog.Warn(ignore, "Unable to dispose transaction");
127126
}
128127

129-
if (session.Factory.Dialect is SQLiteDialect == false)
128+
if (connection != null && session.Factory.Dialect is SQLiteDialect == false)
130129
session.Factory.ConnectionProvider.CloseConnection(connection);
131130
}
132131
}

src/NHibernate/Connection/ConnectionProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public abstract partial class ConnectionProvider : IConnectionProvider
2424
/// <param name="conn">The <see cref="DbConnection"/> to clean up.</param>
2525
public virtual void CloseConnection(DbConnection conn)
2626
{
27+
if (conn == null)
28+
throw new ArgumentNullException(nameof(conn));
29+
2730
log.Debug("Closing connection");
2831
try
2932
{

src/NHibernate/Transaction/AdoNetTransactionFactory.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ public virtual void ExecuteWorkInIsolation(ISessionImplementor session, IIsolate
5757
{
5858
// We make an exception for SQLite and use the session's connection,
5959
// since SQLite only allows one connection to the database.
60-
if (session.Factory.Dialect is SQLiteDialect)
61-
connection = session.Connection;
62-
else
63-
connection = session.Factory.ConnectionProvider.GetConnection();
60+
var connection = session.Factory.Dialect is SQLiteDialect
61+
? session.Connection
62+
: session.Factory.ConnectionProvider.GetConnection();
6463

6564
if (transacted)
6665
{
@@ -136,7 +135,7 @@ public virtual void ExecuteWorkInIsolation(ISessionImplementor session, IIsolate
136135
isolaterLog.Warn(ignore, "Unable to dispose transaction");
137136
}
138137

139-
if (session.Factory.Dialect is SQLiteDialect == false)
138+
if (connection != null && session.Factory.Dialect is SQLiteDialect == false)
140139
session.Factory.ConnectionProvider.CloseConnection(connection);
141140
}
142141
}

0 commit comments

Comments
 (0)