Does a transaction Rollback() have any chance to throw an exception?

asp.net-core c# entity-framework entity-framework-core transactions

Question

Question related to managing trasactions with EF Core:

Does an IDbContextTransaction.Rollback() have any chance to return an exception, so that it could ever make sense to place it inside a try block?

i.e.

    try
    {
        //...
        if(condition)
            transaction.Commit();
        else
            throw methodReturn.Exception;
    }
    catch (Exception e)
    {
        try     // Does this try make any sense?
        {
            transaction.Rollback();
        }
        catch
        {
            // Log
        }
        throw e;
    }

PD: In this specific situation it is not possible to embed the transaction into a using statement, so a manual .Rollback() call is required in the real code.

1
1
10/5/2017 1:00:55 PM

Popular Answer

Most of the times it is not until:

  1. Deadlock Detection
  2. No Internet Connectivity
  3. Logically and Physically consistent connection
  4. Shutdown, Power failure, Unexpected termination

To tackle these situations, you can:

  1. Use the Xact_Abort setting. When SET XACT_ABORT is ON, if a Transact-SQL statement raises a run-time error, the entire transaction is terminated and rolled back. When SET XACT_ABORT is OFF, in some cases only the Transact-SQL statement that raised the error is rolled back and the transaction continues processing. Depending upon the severity of the error, the entire transaction may be rolled back even when SET XACT_ABORT is OFF. OFF is the default setting.

  2. Catch and handle the error, and specify a rollback within the error handling

0
10/5/2017 1:08:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow