Skip to content

Commit e145322

Browse files
committed
Dispose transaction at the end of the TransactionScope life-cycle.
1 parent 6c48231 commit e145322

File tree

1 file changed

+13
-59
lines changed

1 file changed

+13
-59
lines changed

src/NHibernate/Transaction/AdoNetWithDistributedTransactionFactory.cs

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ public void EnlistInDistributedTransactionIfNeeded(ISessionImplementor session)
4040

4141
session.TransactionContext = notification;
4242

43-
transaction.EnlistVolatile(
44-
new TransactionCompletionNotification(session),
45-
EnlistmentOptions.None);
46-
4743
logger.DebugFormat("enlisted into DTC transaction: {0}", transaction.IsolationLevel.ToString());
4844
session.AfterTransactionBegin(null);
4945
}
@@ -108,63 +104,12 @@ void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment)
108104
{
109105
preparingEnlistment.ForceRollback(exception);
110106
}
111-
finally
112-
{
113-
_session = null;
114-
try
115-
{
116-
if (_transaction != null) _transaction.Dispose();
117-
}
118-
finally
119-
{
120-
_transaction = null;
121-
}
122-
}
123107
}
124108
}
125109

126110
void IEnlistmentNotification.Commit(Enlistment enlistment)
127111
{
128-
enlistment.Done();
129-
}
130-
131-
void IEnlistmentNotification.Rollback(Enlistment enlistment)
132-
{
133-
enlistment.Done();
134-
}
135-
136-
void IEnlistmentNotification.InDoubt(Enlistment enlistment)
137-
{
138-
enlistment.Done();
139-
}
140-
141-
public void Dispose()
142-
{
143-
if (!ReferenceEquals(_transaction, null))
144-
_transaction.Dispose();
145-
_transaction = null;
146-
if (!ReferenceEquals(_session, null))
147-
_session.TransactionContext = null;
148-
_session = null;
149-
}
150-
}
151-
152-
class TransactionCompletionNotification : IEnlistmentNotification
153-
{
154-
ISessionImplementor _session;
155-
156-
public TransactionCompletionNotification(ISessionImplementor session)
157-
{
158-
_session = session;
159-
}
160-
161-
void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment)
162-
{
163-
preparingEnlistment.Prepared();
164-
}
165-
166-
void IEnlistmentNotification.Commit(Enlistment enlistment)
167-
{
112+
using (this)
168113
using (new SessionIdLoggingContext(_session.SessionId))
169114
{
170115
try
@@ -178,14 +123,14 @@ void IEnlistmentNotification.Commit(Enlistment enlistment)
178123
}
179124
finally
180125
{
181-
_session = null;
182126
enlistment.Done();
183127
}
184128
}
185129
}
186130

187131
void IEnlistmentNotification.Rollback(Enlistment enlistment)
188132
{
133+
using (this)
189134
using (new SessionIdLoggingContext(_session.SessionId))
190135
{
191136
try
@@ -199,14 +144,14 @@ void IEnlistmentNotification.Rollback(Enlistment enlistment)
199144
}
200145
finally
201146
{
202-
_session = null;
203147
enlistment.Done();
204148
}
205149
}
206150
}
207151

208152
void IEnlistmentNotification.InDoubt(Enlistment enlistment)
209153
{
154+
using (this)
210155
using (new SessionIdLoggingContext(_session.SessionId))
211156
{
212157
try
@@ -220,12 +165,21 @@ void IEnlistmentNotification.InDoubt(Enlistment enlistment)
220165
}
221166
finally
222167
{
223-
_session = null;
224168
enlistment.Done();
225169
}
226170
}
227171
}
228172

173+
public void Dispose()
174+
{
175+
if (!ReferenceEquals(_transaction, null))
176+
_transaction.Dispose();
177+
_transaction = null;
178+
if (!ReferenceEquals(_session, null))
179+
_session.TransactionContext = null;
180+
_session = null;
181+
}
182+
229183
void OnTransactionCompleted(bool successful)
230184
{
231185
var transactionContext = _session.TransactionContext;

0 commit comments

Comments
 (0)