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 Code-- can we reopen Carol's ticket?UPDATE dbo.TicketsSET Status = 'Active'WHERE TicketID = 2 ;(1 row(s) affected)(1 row(s) affected)Msg 50000, Level 16, State 1, Procedure Tickets_Upd, Line 10Cannot change status to Active if the developer in charge ison vacationMsg 3609, Level 16, State 1, Line 10The transaction ended in the trigger. The batch has beenaborted.Listing 4-13: Testing out the Tickets_Upd trigger.So, it looks like our triggers work and our two business rules are enforced. However, sofar we have only tested them from a single connection. What happens when we haveconcurrent access, so our triggers are being fired from multiple connections?To mimic real life concurrency, we'll begin two transactions in two tabs, but not committhem. That will ensure that two modifications are active at the same time. In one tab,run the script shown in Listing 4-14, which first resets the test data to ensure Arnie'sticket is closed, and then starts the test transaction, which will send him on vacation.-- Tab 1-- reset the test data; close Arnie's ticketUPDATE dbo.TicketsSET Status = 'Closed'WHERE TicketID = 1 ;-- start the transaction that sends Arnie on vacationSET TRANSACTION ISOLATION LEVEL READ COMMITTED ;BEGIN TRANSACTION ;116

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

Saved successfully!

Ooh no, something went wrong!