Skip to content

Commit dae3dd2

Browse files
Remove another concurrency issue possibility
1 parent 9c93f48 commit dae3dd2

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

src/NHibernate/ISession.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,11 @@ public partial interface ISession : IDisposable
243243
/// End the <c>ISession</c> by disconnecting from the ADO.NET connection and cleaning up.
244244
/// </summary>
245245
/// <remarks>
246-
/// It is not strictly necessary to <c>Close()</c> the <c>ISession</c> but you must
247-
/// at least <c>Disconnect()</c> it.
246+
/// <para>It is not strictly necessary to <c>Close()</c> the <c>ISession</c> but you must
247+
/// at least <c>Disconnect()</c> or <c>Dispose</c> it.</para>
248+
/// <para>Do not call this method inside a transaction scope, use <c>Dispose</c> instead,
249+
/// since <c>Close()</c> is not aware of system transactions: if the transaction completion
250+
/// requires the session, it will fail.</para>
248251
/// </remarks>
249252
/// <returns>The connection provided by the application or <see langword="null" /></returns>
250253
DbConnection Close();

src/NHibernate/Impl/SessionImpl.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,10 @@ public bool ShouldAutoClose
264264
get { return IsAutoCloseSessionEnabled && !IsClosed; }
265265
}
266266

267-
/// <summary>
268-
/// Close the session and release all resources
269-
/// <remarks>
270-
/// Do not call this method inside a transaction scope, use <c>Dispose</c> instead, since
271-
/// Close() is not aware of distributed transactions
272-
/// </remarks>
273-
/// </summary>
267+
/// <inheritdoc />
274268
public DbConnection Close()
275269
{
276-
using (BeginContext())
270+
using (BeginProcess(true))
277271
{
278272
log.Debug("closing session");
279273
if (IsClosed)
@@ -1498,7 +1492,8 @@ public void Dispose()
14981492
// a local variable for avoiding it, but that would turn a failure causing an exception
14991493
// into a failure causing a session and connection leak. So do not do it, better blow away
15001494
// with a null ref rather than silently leaking a session. And then fix the synchronization.
1501-
if (TransactionContext != null && TransactionContext.CanFlushOnSystemTransactionCompleted)
1495+
if (TransactionContext != null && TransactionContext.CanFlushOnSystemTransactionCompleted &&
1496+
TransactionContext.IsInActiveTransaction)
15021497
{
15031498
TransactionContext.ShouldCloseSessionOnSystemTransactionCompleted = true;
15041499
return;

src/NHibernate/Transaction/AdoNetWithSystemTransactionFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ protected virtual void Lock()
299299
{
300300
if (!_needCompletionLocking || _isDisposed)
301301
return;
302-
_needCompletionLocking = false;
303302
_lock.Reset();
303+
_needCompletionLocking = false;
304304
}
305305

306306
/// <summary>
@@ -583,7 +583,7 @@ private static void Cleanup(ISessionImplementor session)
583583
foreach (var dependentSession in session.ConnectionManager.DependentSessions.ToList())
584584
{
585585
var dependentContext = dependentSession.TransactionContext;
586-
// Do not nullify TransactionContext here, could create a race condition with
586+
// Do not nullify TransactionContext here, it could create a race condition with
587587
// would be await-er on session for disposal (test cases cleanup checks by example).
588588
if (dependentContext == null)
589589
continue;

0 commit comments

Comments
 (0)