@@ -33,7 +33,7 @@ public void EnlistInSystemTransactionIfNeeded(ISessionImplementor session)
33
33
// Ensure the session does not run on a thread supposed to be blocked, waiting
34
34
// for transaction completion.
35
35
session . TransactionContext ? . WaitOne ( ) ;
36
-
36
+
37
37
var transaction = System . Transactions . Transaction . Current ;
38
38
// We may have defined the transaction context before having the connection, do not
39
39
// move under test on TransactionContext already defined. Otherwise we would rely on
@@ -49,11 +49,11 @@ public void EnlistInSystemTransactionIfNeeded(ISessionImplementor session)
49
49
session . TransactionContext = transactionContext ;
50
50
_logger . DebugFormat (
51
51
"enlisted into DTC transaction: {0}" ,
52
- transactionContext . AmbientTransation . IsolationLevel ) ;
52
+ transactionContext . AmbientTransaction . IsolationLevel ) ;
53
53
session . AfterTransactionBegin ( null ) ;
54
54
55
- transactionContext . AmbientTransation . TransactionCompleted += transactionContext . TransactionCompleted ;
56
- transactionContext . AmbientTransation . EnlistVolatile (
55
+ transactionContext . AmbientTransaction . TransactionCompleted += transactionContext . TransactionCompleted ;
56
+ transactionContext . AmbientTransaction . EnlistVolatile (
57
57
transactionContext ,
58
58
EnlistmentOptions . EnlistDuringPrepareRequired ) ;
59
59
}
@@ -74,30 +74,30 @@ public void ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork wo
74
74
75
75
public class SystemTransactionContext : ITransactionContext , IEnlistmentNotification
76
76
{
77
- internal System . Transactions . Transaction AmbientTransation { get ; private set ; }
77
+ internal System . Transactions . Transaction AmbientTransaction { get ; private set ; }
78
78
public bool ShouldCloseSessionOnSystemTransactionCompleted { get ; set ; }
79
79
public bool IsInActiveTransaction { get ; internal set ; }
80
80
81
81
private readonly ISessionImplementor _sessionImplementor ;
82
82
private readonly ManualResetEvent _waitEvent = new ManualResetEvent ( true ) ;
83
83
private volatile bool _locked ;
84
84
private readonly AsyncLocal < bool > _bypassWait = new AsyncLocal < bool > ( ) ;
85
- private bool IsDistributed => AmbientTransation . TransactionInformation . DistributedIdentifier != Guid . Empty ;
85
+ private bool IsDistributed => AmbientTransaction . TransactionInformation . DistributedIdentifier != Guid . Empty ;
86
86
87
87
public SystemTransactionContext (
88
88
ISessionImplementor sessionImplementor ,
89
89
System . Transactions . Transaction transaction )
90
90
{
91
91
_sessionImplementor = sessionImplementor ;
92
- AmbientTransation = transaction . Clone ( ) ;
92
+ AmbientTransaction = transaction . Clone ( ) ;
93
93
IsInActiveTransaction = true ;
94
94
}
95
95
96
96
public void WaitOne ( )
97
97
{
98
98
if ( _bypassWait . Value || _isDisposed )
99
99
return ;
100
- if ( ! _locked && AmbientTransation . TransactionInformation . Status != TransactionStatus . Active )
100
+ if ( ! _locked && AmbientTransaction . TransactionInformation . Status != TransactionStatus . Active )
101
101
// Rollback case may end the transaction without a prepare phase, apply the lock.
102
102
SafeLockSession ( ) ;
103
103
try
@@ -157,7 +157,7 @@ void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment)
157
157
{
158
158
try
159
159
{
160
- using ( var tx = new TransactionScope ( AmbientTransation ) )
160
+ using ( var tx = new TransactionScope ( AmbientTransaction ) )
161
161
{
162
162
if ( _sessionImplementor . FlushMode != FlushMode . Manual && _sessionImplementor . ConnectionManager . IsConnected )
163
163
{
@@ -197,9 +197,6 @@ void IEnlistmentNotification.InDoubt(Enlistment enlistment)
197
197
198
198
private void ProcessSecondPhase ( Enlistment enlistment , bool ? success )
199
199
{
200
- // In case of rollback, the prepare phase may have not be run, and we then need to lock as soon as possible.
201
- // There is no guarantee the second phase will be run before the completed event, doing that at both places.
202
- SafeLockSession ( ) ;
203
200
using ( new SessionIdLoggingContext ( _sessionImplementor . SessionId ) )
204
201
{
205
202
_logger . Debug (
@@ -225,9 +222,6 @@ private void ProcessSecondPhase(Enlistment enlistment, bool? success)
225
222
226
223
public void TransactionCompleted ( object sender , TransactionEventArgs e )
227
224
{
228
- // In case of rollback, the prepare phase may have not be run, and we then need to lock as soon as possible.
229
- // There is no guarantee the second phase will be run before the completed event, doing that at both places.
230
- SafeLockSession ( ) ;
231
225
e . Transaction . TransactionCompleted -= TransactionCompleted ;
232
226
// This event may execute before second phase, so we cannot try to get the success from second phase.
233
227
// Using this event is required by example in case the prepare phase failed and called force rollback:
@@ -237,7 +231,7 @@ public void TransactionCompleted(object sender, TransactionEventArgs e)
237
231
try
238
232
{
239
233
wasSuccessful =
240
- e . Transaction . TransactionInformation . Status == TransactionStatus . Committed ;
234
+ AmbientTransaction . TransactionInformation . Status == TransactionStatus . Committed ;
241
235
}
242
236
catch ( ObjectDisposedException ode )
243
237
{
@@ -253,6 +247,7 @@ private void RunAfterTransactionActions(bool wasSuccessful)
253
247
if ( _afterTransactionActionsDone )
254
248
// Probably called from In-Doubt and TransactionCompleted.
255
249
return ;
250
+ _afterTransactionActionsDone = true ;
256
251
// Allow transaction completed actions to run while others stay blocked.
257
252
_bypassWait . Value = true ;
258
253
try
@@ -272,7 +267,6 @@ private void RunAfterTransactionActions(bool wasSuccessful)
272
267
}
273
268
finally
274
269
{
275
- _afterTransactionActionsDone = true ;
276
270
// Dispose releases blocked threads by the way.
277
271
// Must dispose in case !ShouldCloseSessionOnSystemTransactionCompleted, since
278
272
// we nullify session TransactionContext, causing it to have nothing still holding it.
@@ -296,11 +290,8 @@ protected virtual void Dispose(bool disposing)
296
290
{
297
291
if ( disposing )
298
292
{
299
- if ( AmbientTransation != null )
300
- {
301
- AmbientTransation . Dispose ( ) ;
302
- AmbientTransation = null ;
303
- }
293
+ AmbientTransaction ? . Dispose ( ) ;
294
+ AmbientTransaction = null ;
304
295
_waitEvent . Set ( ) ;
305
296
_waitEvent . Dispose ( ) ;
306
297
}
0 commit comments