@@ -348,31 +348,34 @@ public void SinglePhaseCommit(SinglePhaseEnlistment enlistment)
348348#endif
349349 try
350350 {
351- Exception commitException = null ;
352-
353- lock ( connection )
351+ // If the connection is doomed, we can be certain that the
352+ // transaction will eventually be rolled back or has already been aborted externally, and we shouldn't
353+ // attempt to commit it.
354+ if ( connection . IsConnectionDoomed )
354355 {
355- // If the connection is doomed, we can be certain that the
356- // transaction will eventually be rolled back or has already been aborted externally, and we shouldn't
357- // attempt to commit it.
358- if ( connection . IsConnectionDoomed )
356+ lock ( connection )
359357 {
360358 _active = false ; // set to inactive first, doesn't matter how the rest completes, this transaction is done.
361359 _connection = null ;
362-
363- enlistment . Aborted ( SQL . ConnectionDoomed ( ) ) ;
364360 }
365- else
361+
362+ enlistment . Aborted ( SQL . ConnectionDoomed ( ) ) ;
363+ }
364+ else
365+ {
366+ Exception commitException ;
367+ lock ( connection )
366368 {
367369 try
368370 {
369371 // Now that we've acquired the lock, make sure we still have valid state for this operation.
370372 ValidateActiveOnConnection ( connection ) ;
371373
372374 _active = false ; // set to inactive first, doesn't matter how the rest completes, this transaction is done.
373- _connection = null ; // Set prior to ExecuteTransaction call in case this initiates a TransactionEnd event
375+ _connection = null ; // Set prior to ExecuteTransaction call in case this initiates a TransactionEnd event
374376
375377 connection . ExecuteTransaction ( SqlInternalConnection . TransactionRequest . Commit , null , System . Data . IsolationLevel . Unspecified , _internalTransaction , true ) ;
378+ commitException = null ;
376379 }
377380 catch ( SqlException e )
378381 {
@@ -391,42 +394,41 @@ public void SinglePhaseCommit(SinglePhaseEnlistment enlistment)
391394 ADP . TraceExceptionWithoutRethrow ( e ) ;
392395 connection . DoomThisConnection ( ) ;
393396 }
394- if ( commitException != null )
397+ }
398+ if ( commitException != null )
399+ {
400+ // connection.ExecuteTransaction failed with exception
401+ if ( _internalTransaction . IsCommitted )
395402 {
396- // connection.ExecuteTransaction failed with exception
397- if ( _internalTransaction . IsCommitted )
398- {
399- // Even though we got an exception, the transaction
400- // was committed by the server.
401- enlistment . Committed ( ) ;
402- }
403- else if ( _internalTransaction . IsAborted )
404- {
405- // The transaction was aborted, report that to
406- // SysTx.
407- enlistment . Aborted ( commitException ) ;
408- }
409- else
410- {
411- // The transaction is still active, we cannot
412- // know the state of the transaction.
413- enlistment . InDoubt ( commitException ) ;
414- }
415-
416- // We eat the exception. This is called on the SysTx
417- // thread, not the applications thread. If we don't
418- // eat the exception an UnhandledException will occur,
419- // causing the process to FailFast.
403+ // Even though we got an exception, the transaction
404+ // was committed by the server.
405+ enlistment . Committed ( ) ;
406+ }
407+ else if ( _internalTransaction . IsAborted )
408+ {
409+ // The transaction was aborted, report that to
410+ // SysTx.
411+ enlistment . Aborted ( commitException ) ;
412+ }
413+ else
414+ {
415+ // The transaction is still active, we cannot
416+ // know the state of the transaction.
417+ enlistment . InDoubt ( commitException ) ;
420418 }
421419
422- connection . CleanupConnectionOnTransactionCompletion ( _atomicTransaction ) ;
420+ // We eat the exception. This is called on the SysTx
421+ // thread, not the applications thread. If we don't
422+ // eat the exception an UnhandledException will occur,
423+ // causing the process to FailFast.
423424 }
424- }
425425
426- if ( commitException == null )
427- {
428- // connection.ExecuteTransaction succeeded
429- enlistment . Committed ( ) ;
426+ connection . CleanupConnectionOnTransactionCompletion ( _atomicTransaction ) ;
427+ if ( commitException == null )
428+ {
429+ // connection.ExecuteTransaction succeeded
430+ enlistment . Committed ( ) ;
431+ }
430432 }
431433 }
432434 catch ( System . OutOfMemoryException e )
0 commit comments