@@ -36,31 +36,38 @@ public void EnlistInDistributedTransactionIfNeeded(ISessionImplementor session)
3636logger . DebugFormat ( "enlisted into DTC transaction: {0}" ,
3737 transactionContext . AmbientTransation . IsolationLevel ) ;
3838session . AfterTransactionBegin ( null ) ;
39- transactionContext . AmbientTransation . TransactionCompleted +=
40- delegate ( object sender , TransactionEventArgs e )
39+
40+ TransactionCompletedEventHandler handler = null ;
41+
42+ handler = delegate ( object sender , TransactionEventArgs e )
43+ {
44+ using ( new SessionIdLoggingContext ( session . SessionId ) )
4145{
42- using ( new SessionIdLoggingContext ( session . SessionId ) )
46+ ( ( DistributedTransactionContext ) session . TransactionContext ) . IsInActiveTransaction = false ;
47+
48+ bool wasSuccessful = false ;
49+ try
4350{
44- ( ( DistributedTransactionContext ) session . TransactionContext ) . IsInActiveTransaction = false ;
45-
46- bool wasSuccessful = false ;
47- try
48- {
49- wasSuccessful = e . Transaction . TransactionInformation . Status
50- == TransactionStatus . Committed ;
51- }
52- catch ( ObjectDisposedException ode )
53- {
54- logger . Warn ( "Completed transaction was disposed, assuming transaction rollback" , ode ) ;
55- }
56- session . AfterTransactionCompletion ( wasSuccessful , null ) ;
57- if ( transactionContext . ShouldCloseSessionOnDistributedTransactionCompleted )
58- {
59- session . CloseSessionFromDistributedTransaction ( ) ;
60- }
61- session . TransactionContext = null ;
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 ( ) ;
6262}
63- } ;
63+ session . TransactionContext = null ;
64+ }
65+
66+ e . Transaction . TransactionCompleted -= handler ;
67+ } ;
68+
69+ transactionContext . AmbientTransation . TransactionCompleted += handler ;
70+
6471transactionContext . AmbientTransation . EnlistVolatile ( transactionContext ,
6572EnlistmentOptions . EnlistDuringPrepareRequired ) ;
6673}
0 commit comments