Skip to content

Commit 638f148

Browse files
committed
Clean up code.
1 parent 46b5b2c commit 638f148

File tree

3 files changed

+55
-83
lines changed

3 files changed

+55
-83
lines changed

src/NHibernate.Test/NHSpecificTest/NH2420/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void ShouldBeAbleToReleaseSuppliedConnectionAfterDistributedTransaction()
9090
// fires *after* the transaction is committed and so it doesn't affect the success
9191
// of the transaction.
9292

93-
Assert.That(s.IsConnected, Is.False);
93+
Assert.That(() => s.IsConnected, Is.False.After(500, 100));
9494
Assert.That(((ISessionImplementor)s).ConnectionManager.IsConnected, Is.False);
9595
Assert.That(((ISessionImplementor)s).IsClosed, Is.True);
9696
}

src/NHibernate/Impl/AbstractSessionImpl.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ protected internal void SetClosed()
330330
{
331331
if (TransactionContext != null)
332332
TransactionContext.Dispose();
333-
TransactionContext = null;
334333
}
335334
catch (Exception)
336335
{
Lines changed: 54 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Threading;
34
using System.Transactions;
45
using NHibernate.Engine;
56
using NHibernate.Engine.Transaction;
@@ -29,54 +30,14 @@ public void EnlistInDistributedTransactionIfNeeded(ISessionImplementor session)
2930

3031
if (System.Transactions.Transaction.Current == null)
3132
return;
32-
33-
var transactionContext = new DistributedTransactionContext(session,
34-
System.Transactions.Transaction.Current);
35-
session.TransactionContext = transactionContext;
36-
logger.DebugFormat("enlisted into DTC transaction: {0}",
37-
transactionContext.AmbientTransation.IsolationLevel);
38-
session.AfterTransactionBegin(null);
39-
40-
TransactionCompletedEventHandler handler = null;
41-
42-
handler = delegate(object sender, TransactionEventArgs e)
43-
{
44-
using (new SessionIdLoggingContext(session.SessionId))
45-
{
46-
((DistributedTransactionContext) session.TransactionContext).IsInActiveTransaction = false;
47-
48-
bool wasSuccessful = false;
49-
try
50-
{
51-
wasSuccessful = e.Transaction.TransactionInformation.Status
52-
== TransactionStatus.Committed;
53-
}
54-
catch (ObjectDisposedException ode)
55-
{
56-
logger.Warn("Completed transaction was disposed, assuming transaction rollback", ode);
57-
}
58-
session.AfterTransactionCompletion(wasSuccessful, null);
59-
if (transactionContext.ShouldCloseSessionOnDistributedTransactionCompleted)
60-
{
61-
session.CloseSessionFromDistributedTransaction();
62-
}
63-
session.TransactionContext = null;
64-
}
65-
66-
e.Transaction.TransactionCompleted -= handler;
67-
};
6833

69-
transactionContext.AmbientTransation.TransactionCompleted += handler;
70-
71-
transactionContext.AmbientTransation.EnlistVolatile(transactionContext,
72-
EnlistmentOptions.EnlistDuringPrepareRequired);
34+
new DistributedTransactionContext(session, System.Transactions.Transaction.Current);
7335
}
7436

7537
public bool IsInDistributedActiveTransaction(ISessionImplementor session)
7638
{
77-
var distributedTransactionContext = ((DistributedTransactionContext)session.TransactionContext);
78-
return distributedTransactionContext != null &&
79-
distributedTransactionContext.IsInActiveTransaction;
39+
var distributedTransactionContext = (DistributedTransactionContext)session.TransactionContext;
40+
return distributedTransactionContext != null;
8041
}
8142

8243
public void ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork work, bool transacted)
@@ -92,35 +53,37 @@ public void ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork wo
9253

9354
public class DistributedTransactionContext : ITransactionContext, IEnlistmentNotification
9455
{
95-
public System.Transactions.Transaction AmbientTransation { get; set; }
96-
public bool ShouldCloseSessionOnDistributedTransactionCompleted { get; set; }
97-
private ISessionImplementor sessionImplementor;
98-
public bool IsInActiveTransaction;
56+
public bool ShouldCloseSessionOnDistributedTransactionCompleted { get; set; }
57+
58+
System.Transactions.Transaction _transaction;
59+
ISessionImplementor _session;
9960

100-
public DistributedTransactionContext(ISessionImplementor sessionImplementor, System.Transactions.Transaction transaction)
61+
public DistributedTransactionContext(ISessionImplementor session, System.Transactions.Transaction transaction)
10162
{
102-
this.sessionImplementor = sessionImplementor;
103-
AmbientTransation = transaction.Clone();
104-
IsInActiveTransaction = true;
63+
_session = session;
64+
_session.TransactionContext = this;
65+
_transaction = transaction.Clone();
66+
logger.DebugFormat("enlisted into DTC transaction: {0}",
67+
_transaction.IsolationLevel);
68+
_session.AfterTransactionBegin(null);
69+
_transaction.EnlistVolatile(this, EnlistmentOptions.EnlistDuringPrepareRequired);
10570
}
10671

107-
#region IEnlistmentNotification Members
108-
10972
void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment)
11073
{
111-
using (new SessionIdLoggingContext(sessionImplementor.SessionId))
74+
using (new SessionIdLoggingContext(_session.SessionId))
11275
{
11376
try
11477
{
115-
using (var tx = new TransactionScope(AmbientTransation))
78+
using (var tx = new TransactionScope(_transaction))
11679
{
117-
sessionImplementor.BeforeTransactionCompletion(null);
118-
if (sessionImplementor.FlushMode != FlushMode.Never && sessionImplementor.ConnectionManager.IsConnected)
80+
_session.BeforeTransactionCompletion(null);
81+
if (_session.FlushMode != FlushMode.Never && _session.ConnectionManager.IsConnected)
11982
{
120-
using (sessionImplementor.ConnectionManager.FlushingFromDtcTransaction)
83+
using (_session.ConnectionManager.FlushingFromDtcTransaction)
12184
{
122-
logger.Debug(string.Format("[session-id={0}] Flushing from Dtc Transaction", sessionImplementor.SessionId));
123-
sessionImplementor.Flush();
85+
logger.Debug(String.Format("[session-id={0}] Flushing from Dtc Transaction", _session.SessionId));
86+
_session.Flush();
12487
}
12588
}
12689
logger.Debug("prepared for DTC transaction");
@@ -132,55 +95,65 @@ void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment)
13295
catch (Exception exception)
13396
{
13497
logger.Error("DTC transaction prepare phase failed", exception);
98+
OnTransactionCompletion(false);
13599
preparingEnlistment.ForceRollback(exception);
136100
}
137101
}
138102
}
139103

140104
void IEnlistmentNotification.Commit(Enlistment enlistment)
141105
{
142-
using (new SessionIdLoggingContext(sessionImplementor.SessionId))
106+
using (new SessionIdLoggingContext(_session.SessionId))
143107
{
144108
logger.Debug("committing DTC transaction");
145-
// we have nothing to do here, since it is the actual
146-
// DB connection that will commit the transaction
147-
enlistment.Done();
148-
IsInActiveTransaction = false;
109+
OnTransactionCompletion(true);
149110
}
111+
enlistment.Done();
150112
}
151113

152114
void IEnlistmentNotification.Rollback(Enlistment enlistment)
153115
{
154-
using (new SessionIdLoggingContext(sessionImplementor.SessionId))
116+
using (new SessionIdLoggingContext(_session.SessionId))
155117
{
156118
logger.Debug("rolled back DTC transaction");
157-
// Currently AfterTransactionCompletion is called by the handler for the TransactionCompleted event.
158-
//sessionImplementor.AfterTransactionCompletion(false, null);
159-
enlistment.Done();
160-
IsInActiveTransaction = false;
119+
OnTransactionCompletion(false);
161120
}
121+
enlistment.Done();
162122
}
163123

164124
void IEnlistmentNotification.InDoubt(Enlistment enlistment)
165125
{
166-
using (new SessionIdLoggingContext(sessionImplementor.SessionId))
126+
using (new SessionIdLoggingContext(_session.SessionId))
167127
{
168-
sessionImplementor.AfterTransactionCompletion(false, null);
169128
logger.Debug("DTC transaction is in doubt");
170-
enlistment.Done();
171-
IsInActiveTransaction = false;
129+
OnTransactionCompletion(false);
172130
}
131+
enlistment.Done();
173132
}
174133

175-
#endregion
176-
177134
public void Dispose()
178135
{
179-
if (AmbientTransation != null)
180-
AmbientTransation.Dispose();
181-
AmbientTransation = null;
182-
sessionImplementor = null;
183-
}
136+
if (_transaction != null)
137+
_transaction.Dispose();
138+
_transaction = null;
139+
if (_session != null)
140+
_session.TransactionContext = null;
141+
_session = null;
142+
}
143+
144+
void OnTransactionCompletion(bool wasSuccessful)
145+
{
146+
using (new SessionIdLoggingContext(_session.SessionId))
147+
{
148+
_session.TransactionContext = null;
149+
_session.AfterTransactionCompletion(wasSuccessful, null);
150+
if (ShouldCloseSessionOnDistributedTransactionCompleted)
151+
{
152+
_session.CloseSessionFromDistributedTransaction();
153+
}
154+
Dispose();
155+
}
156+
}
184157
}
185158
}
186159
}

0 commit comments

Comments
 (0)