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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: When Upgrading Breaks CodeOur test data includes one developer (Arnie) who is currently active and has one activeticket assigned to him, so we can test what happens if Arnie attempts to go on holidaywithout assigning his ticket to someone else. Apparently, under READ COMMITTEDisolation level, our Developers_Upd trigger prevents violation of this business rule, asshown in Listing 4-12.SET TRANSACTION ISOLATION LEVEL READ COMMITTED ;UPDATE dbo.DevelopersSET Status = 'Vacation'WHERE DeveloperID = 1 ;Msg 50000, Level 16, State 1, Procedure Developers_Upd, Line11Developers must assign their active tickets to someone elsebefore going on vacationMsg 3609, Level 16, State 1, Line 2The transaction ended in the trigger. The batch has beenaborted.Listing 4-12: Testing the Developers_Upd trigger.Similarly, our dbo.Tickets_Upd trigger ensures that inactive tickets cannot bechanged to active status if they are assigned to developers who are on vacation, as shownin Listing 4-13.SET TRANSACTION ISOLATION LEVEL READ COMMITTED ;-- make sure Carol's ticket is closedUPDATE dbo.TicketsSET Status = 'Closed'WHERE TicketID = 2 ;-- Carol can now go on vacationUPDATE dbo.DevelopersSET Status = 'Vacation'WHERE DeveloperID = 2 ;115

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

Saved successfully!

Ooh no, something went wrong!