@@ -348,31 +348,36 @@ 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 = null ;
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
- _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
374
+ _active =
375
+ false ; // set to inactive first, doesn't matter how the rest completes, this transaction is done.
376
+ _connection =
377
+ null ; // Set prior to ExecuteTransaction call in case this initiates a TransactionEnd event
374
378
375
- connection . ExecuteTransaction ( SqlInternalConnection . TransactionRequest . Commit , null , System . Data . IsolationLevel . Unspecified , _internalTransaction , true ) ;
379
+ connection . ExecuteTransaction ( SqlInternalConnection . TransactionRequest . Commit , null ,
380
+ System . Data . IsolationLevel . Unspecified , _internalTransaction , true ) ;
376
381
}
377
382
catch ( SqlException e )
378
383
{
@@ -391,42 +396,42 @@ public void SinglePhaseCommit(SinglePhaseEnlistment enlistment)
391
396
ADP . TraceExceptionWithoutRethrow ( e ) ;
392
397
connection . DoomThisConnection ( ) ;
393
398
}
394
- if ( commitException != null )
399
+ }
400
+ if ( commitException != null )
401
+ {
402
+ // connection.ExecuteTransaction failed with exception
403
+ if ( _internalTransaction . IsCommitted )
395
404
{
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.
405
+ // Even though we got an exception, the transaction
406
+ // was committed by the server.
407
+ enlistment . Committed ( ) ;
408
+ }
409
+ else if ( _internalTransaction . IsAborted )
410
+ {
411
+ // The transaction was aborted, report that to
412
+ // SysTx.
413
+ enlistment . Aborted ( commitException ) ;
414
+ }
415
+ else
416
+ {
417
+ // The transaction is still active, we cannot
418
+ // know the state of the transaction.
419
+ enlistment . InDoubt ( commitException ) ;
420
420
}
421
421
422
- connection . CleanupConnectionOnTransactionCompletion ( _atomicTransaction ) ;
422
+ // We eat the exception. This is called on the SysTx
423
+ // thread, not the applications thread. If we don't
424
+ // eat the exception an UnhandledException will occur,
425
+ // causing the process to FailFast.
423
426
}
424
- }
425
427
426
- if ( commitException == null )
427
- {
428
- // connection.ExecuteTransaction succeeded
429
- enlistment . Committed ( ) ;
428
+ connection . CleanupConnectionOnTransactionCompletion ( _atomicTransaction ) ;
429
+
430
+ if ( commitException == null )
431
+ {
432
+ // connection.ExecuteTransaction succeeded
433
+ enlistment . Committed ( ) ;
434
+ }
430
435
}
431
436
}
432
437
catch ( System . OutOfMemoryException e )
0 commit comments