Commitable transaction after error?

  • The URL

    http://msdn.microsoft.com/en-us/library/ms189797.aspx

    shows a TRY...CATCH block containing a transaction. Here's an extract from the CATCH block:

    >>>>

    -- Test whether the transaction is active and valid.

    IF (XACT_STATE()) = 1

    BEGIN

    PRINT 'The transaction is committable.' +

    ' Committing transaction.'

    COMMIT TRANSACTION;

    END;

    >>>>

    I find this very confusing! We are in a CATCH block -- so an error has happened -- yet we are COMMITing a transaction ?? How is this possible? It seems that we are "committing" what we can and ignoring the error. Doesn't this possibly create inconsistent table data?

    TIA,

    barklingdog

  • Hi Barkingdog

    It depends... Maybe you have a procedure which first INSERTs a record into a table and after this you are doing some logging or things like that. If your INSERT is the main action and it might be possible that the other actions miss I could be okay to commit the transaction anyway.

    Sure you are right usually you wont commit within the CATCH block. It's just an example for the XACT_STATE ;-).

    Greets

    Flo

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply