@@ -348,31 +348,34 @@ public void SinglePhaseCommit(SinglePhaseEnlistment enlistment)
348
348
#endif
349
349
try
350
350
{
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 )
354
355
{
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 )
359
357
{
360
358
_active = false ; // set to inactive first, doesn't matter how the rest completes, this transaction is done.
361
359
_connection = null ;
362
-
363
- enlistment . Aborted ( SQL . ConnectionDoomed ( ) ) ;
364
360
}
365
- else
361
+
362
+ enlistment . Aborted ( SQL . ConnectionDoomed ( ) ) ;
363
+ }
364
+ else
365
+ {
366
+ Exception commitException ;
367
+ lock ( connection )
366
368
{
367
369
try
368
370
{
369
371
// Now that we've acquired the lock, make sure we still have valid state for this operation.
370
372
ValidateActiveOnConnection ( connection ) ;
371
373
372
374
_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
374
376
375
377
connection . ExecuteTransaction ( SqlInternalConnection . TransactionRequest . Commit , null , System . Data . IsolationLevel . Unspecified , _internalTransaction , true ) ;
378
+ commitException = null ;
376
379
}
377
380
catch ( SqlException e )
378
381
{
@@ -391,42 +394,41 @@ public void SinglePhaseCommit(SinglePhaseEnlistment enlistment)
391
394
ADP . TraceExceptionWithoutRethrow ( e ) ;
392
395
connection . DoomThisConnection ( ) ;
393
396
}
394
- if ( commitException != null )
397
+ }
398
+ if ( commitException != null )
399
+ {
400
+ // connection.ExecuteTransaction failed with exception
401
+ if ( _internalTransaction . IsCommitted )
395
402
{
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 ) ;
420
418
}
421
419
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.
423
424
}
424
- }
425
425
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
+ }
430
432
}
431
433
}
432
434
catch ( System . OutOfMemoryException e )
0 commit comments