Skip to content

Commit 7035c55

Browse files
Do not leave AdoTransaction in an inconsistent state
Fix #2871
1 parent 5282202 commit 7035c55

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

src/NHibernate/Async/Transaction/AdoTransaction.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,29 @@ protected virtual async Task DisposeAsync(bool isDisposing, CancellationToken ca
170170
// know this call came through Dispose()
171171
if (isDisposing)
172172
{
173-
if (trans != null)
173+
try
174174
{
175-
trans.Dispose();
176-
trans = null;
177-
log.Debug("DbTransaction disposed.");
178-
}
175+
if (trans != null)
176+
{
177+
trans.Dispose();
178+
trans = null;
179+
log.Debug("DbTransaction disposed.");
180+
}
179181

180-
if (IsActive && session != null)
182+
if (IsActive && session != null)
183+
{
184+
// Assume we are rolled back
185+
await (AfterTransactionCompletionAsync(false, cancellationToken)).ConfigureAwait(false);
186+
}
187+
// nothing for Finalizer to do - so tell the GC to ignore it
188+
GC.SuppressFinalize(this);
189+
}
190+
finally
181191
{
182-
// Assume we are rolled back
183-
await (AfterTransactionCompletionAsync(false, cancellationToken)).ConfigureAwait(false);
192+
// Do not leave the object in an inconsistent state in case of disposal failure: we should assume
193+
// the DbTransaction is either no more ongoing or unrecoverable.
194+
begun = false;
184195
}
185-
// nothing for Finalizer to do - so tell the GC to ignore it
186-
GC.SuppressFinalize(this);
187196
}
188197

189198
// free unmanaged resources here

src/NHibernate/Transaction/AdoTransaction.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,20 +376,29 @@ protected virtual void Dispose(bool isDisposing)
376376
// know this call came through Dispose()
377377
if (isDisposing)
378378
{
379-
if (trans != null)
379+
try
380380
{
381-
trans.Dispose();
382-
trans = null;
383-
log.Debug("DbTransaction disposed.");
381+
if (trans != null)
382+
{
383+
trans.Dispose();
384+
trans = null;
385+
log.Debug("DbTransaction disposed.");
386+
}
387+
388+
if (IsActive && session != null)
389+
{
390+
// Assume we are rolled back
391+
AfterTransactionCompletion(false);
392+
}
393+
// nothing for Finalizer to do - so tell the GC to ignore it
394+
GC.SuppressFinalize(this);
384395
}
385-
386-
if (IsActive && session != null)
396+
finally
387397
{
388-
// Assume we are rolled back
389-
AfterTransactionCompletion(false);
398+
// Do not leave the object in an inconsistent state in case of disposal failure: we should assume
399+
// the DbTransaction is either no more ongoing or unrecoverable.
400+
begun = false;
390401
}
391-
// nothing for Finalizer to do - so tell the GC to ignore it
392-
GC.SuppressFinalize(this);
393402
}
394403

395404
// free unmanaged resources here

0 commit comments

Comments
 (0)