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 4: When Upgrading Breaks CodeUPDATE dbo.DevelopersSET Status = 'Vacation'WHERE DeveloperID = 1 ;-- COMMIT ;Listing 4-14: Arnie is on vacation, but the change has not committed yet.Note that, when this script has finished, the transaction opened by this connection isstill outstanding, because we have neither committed it nor rolled it back. In a secondtab, try to reopen the ticket assigned to Arnie, as shown in Listing 4-15.-- Tab 2SET TRANSACTION ISOLATION LEVEL READ COMMITTED ;BEGIN TRANSACTION ;UPDATE dbo.TicketsSET Status = 'Active'WHERE TicketID = 1 ;-- COMMIT ;Listing 4-15: Attempting to reopen a ticket assigned to Arnie.This script will not complete; it will stay in lock-waiting state, because theTickets_Upd trigger needs to read the Developers table and find out whetherArnie is active or on vacation, and that row has been changed and is locked by thetransaction in the first tab (Listing 4-14).Go back to the first tab, and commit the outstanding transaction. The script in thesecond tab will immediately fail with the same error message as shown in Listing 4-13,which is the expected behavior.117

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

Saved successfully!

Ooh no, something went wrong!