Skip to content

Commit 0670a96

Browse files
committed
NH-3807 - Check that enlisted transactions were disposed
1 parent 28d69e6 commit 0670a96

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/NHibernate.Test/TestCase.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,15 @@ public void TearDown()
162162
var wereClosed = _sessionFactory.CheckSessionsWereClosed();
163163
var wasCleaned = CheckDatabaseWasCleaned();
164164
var wereConnectionsClosed = CheckConnectionsWereClosed();
165-
fail = !wereClosed || !wasCleaned || !wereConnectionsClosed;
165+
var wereTransactionsDisposed = CheckEnlistedTransactionsWereDisposed();
166+
fail = !wereClosed || !wasCleaned || !wereConnectionsClosed || !wereTransactionsDisposed;
166167

167168
if (fail)
168169
{
169170
badCleanupMessage = "Test didn't clean up after itself. session closed: " + wereClosed + "; database cleaned: " +
170171
wasCleaned
171-
+ "; connection closed: " + wereConnectionsClosed;
172+
+ "; connection closed: " + wereConnectionsClosed
173+
+ "; transactions disposed:" + wereTransactionsDisposed;;
172174
if (testResult != null && testResult.Outcome.Status == TestStatus.Failed)
173175
{
174176
// Avoid hiding a test failure (asserts are usually not hidden, but other exception would be).
@@ -259,6 +261,31 @@ private bool CheckConnectionsWereClosed()
259261
return false;
260262
}
261263

264+
private bool CheckEnlistedTransactionsWereDisposed()
265+
{
266+
System.Transactions.Transaction current = System.Transactions.Transaction.Current;
267+
bool notAborted = ((System.Transactions.Transaction.Current?.TransactionInformation.Status) ?? System.Transactions.TransactionStatus.Aborted) != System.Transactions.TransactionStatus.Aborted;
268+
if (!notAborted) return true;
269+
270+
do
271+
{
272+
notAborted = current.TransactionInformation.Status != System.Transactions.TransactionStatus.Aborted;
273+
274+
try
275+
{
276+
current.Dispose();
277+
}
278+
catch (Exception ex)
279+
{
280+
log.Error("Error disposing enlisted transaction", ex);
281+
}
282+
283+
current = System.Transactions.Transaction.Current;
284+
} while (current != null && notAborted);
285+
286+
return false;
287+
}
288+
262289
/// <summary>
263290
/// (Re)Create the configuration.
264291
/// </summary>

0 commit comments

Comments
 (0)