17.07.2015 Views

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8: <strong>Defensive</strong> Error HandlingPRINT 'Modifications succeeded' ;COMMIT ;RETURN 0 ;END TRYBEGIN CATCH-- transaction is not rolled back automatically-- we need to roll back explicitlyIF @@TRANCOUNT 0BEGIN ;PRINT 'Rolling back' ;ROLLBACK ;END ;IF ERROR_NUMBER() 1205BEGIN-- if this is not a deadlock, "re-throw" the errorDECLARE @ErrorMessage NVARCHAR(4000) ;SET @ErrorMessage = ERROR_MESSAGE() ;RAISERROR('Error %s occurred inSelectCodeChangeLogAndCode',16,1,@ErrorMessage) ;RETURN -1 ;END ;ELSEBEGIN ;PRINT 'Encountered a deadlock'END ;END CATCH ;SET @tryCount = @tryCount + 1 ;END ;RETURN 0 ;END ;Listing 8-8: Altering the ChangeCodeDescription stored procedure so that itretries after a deadlock.Before we run our test, let's reset the test data in our Codes and CodeDescriptions-ChangeLog tables.269

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!