Lab Answer Key for Module 11: Managing Transactions and Locks Table of Contents Lab 11: Managing Transactions and Locks 1 Exercise 1: Using Transactions 1 Exercise 2: Managing Locks 3
Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e- mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. The names of manufacturers, products, or URLs are provided for informational purposes only and Microsoft makes no representations and warranties, either expressed, implied, or statutory, regarding these manufacturers or the use of the products with any Microsoft technologies. The inclusion of a manufacturer or product does not imply endorsement of Microsoft of the manufacturer or product. Links are provided to third party sites. Such sites are not under the control of Microsoft and Microsoft is not responsible for the contents of any linked site or any link contained in a linked site, or any changes or updates to such sites. Microsoft is not responsible for webcasting or any other form of transmission received from any linked site. Microsoft is providing these links to you only as a convenience, and the inclusion of any link does not imply endorsement of Microsoft of the site or the products contained therein. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. 2007 Microsoft Corporation. All rights reserved. Microsoft, BizTalk, Internet Explorer, Jscript, MSDN, Outlook, PowerPoint, SQL Server, Visual Basic, Visual C#, Visual C++, Visual FoxPro, Visual Studio, Windows and Windows Server are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Version 1.0
Lab Answer Key for Module 11: Managing Transactions and Locks 1 Lab 11: Managing Transactions and Locks Exercise 1: Using Transactions Task 1: Create and commit a transaction 1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then click SQL Server Management Studio. 2. In the Connect to Server dialog box, specify the values in the following table, and then click Connect. Server type Database Engine 3. On the File menu, point to Open, and then click File. Windows 4. In the Open File dialog box, browse to the D:\Labfiles\Starter folder, click Tran1.sql, 5. In the Connect to Database Engine dialog box, specify the values in the following Windows Notice that the statements update a record in the Person.Contact table. The SELECT statements and the @@trancount global variable are used to show the progress of the transaction. 6. Locate the comment START TRANSACTION HERE, and then add a statement to start a transaction. Your code should resemble the following. BEGIN TRANSACTION 7. Click Execute to execute the script, and then review the results. At this point, the transaction is still active and locks are still held. The changes made have not been committed to the database. 8. Locate the comment END TRANSACTION HERE, and then add a statement to commit the changes to the database. Your code should resemble the following. COMMIT TRANSACTION 9. Select the end transaction statement, and then click Execute. The changes are now committed to the database. 10. Select the last two statements in the script, and then click Execute.
2 Lab Answer Key for Module 11: Managing Transactions and Locks 11. Verify that the transaction count is now set to zero and the changes have been committed to the database. 12. On the File menu, click Save Tran1.sql. 13. On the File menu, click Close. Task 2: Create and roll back a transaction 1. On the File menu, point to Open, and then click File. 2. In the Open File dialog box, browse to the D:\Labfiles\Starter folder, click Tran2.sql, 3. In the Connect to Database Engine dialog box, specify the values in the following Windows Notice that the statements update a different record in the Person.Contact table. Again, the SELECT statements and the @@trancount global variable show the progress of the transaction. 4. Locate the comment END TRANSACTION HERE, and then add a statement to roll back the changes to the database. Your code should resemble the following. ROLLBACK TRANSACTION 5. Click Execute to execute the script, and then review the results. Notice that the transaction is complete because the transaction count is zero and the changes have been cancelled. 6. On the File menu, click Save Tran2.sql. 7. Close SQL Server Management Studio.
Lab Answer Key for Module 11: Managing Transactions and Locks 3 Exercise 2: Managing Locks Task 1: View locking information 1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then click SQL Server Management Studio. 2. In the Connect to Server dialog box, specify the values in the following table, and then click Connect. Server type Database Engine Windows 3. In SQL Server Management Studio, in the console tree, expand, expand Management, right-click Activity Monitor, and then click View Processes. 4. Review the information that is displayed in Process Info, Locks by Process, and Locks by Object. 5. Close Activity Monitor. 6. On the File menu, point to New, and then click Query with Current Connection. 7. Type the following statement into the query window. SELECT resource_type, request_mode, request_type, request_status, request_session_id FROM sys.dm_tran_locks 8. Click Execute to execute the statement. 9. Review the information that shows the current lock in the master database. 10. On the File menu, point to Open, and then click File. 11. In the Open File dialog box, browse to the D:\Labfiles\Starter folder, click Lock1.sql, 12. In the Connect to Database Engine dialog box, specify the values in the following Windows Notice that a transaction starts with the BEGIN TRANSACTION statement, but there is no corresponding COMMIT TRANSACTION or ROLLBACK TRANSACTION statement to complete the transaction. 13. Click Execute to execute the script and note the SPID of the connection. 14. Switch back to the original query window and then click Execute to execute the query and retrieve data from the sys.dm_tran_locks view.
4 Lab Answer Key for Module 11: Managing Transactions and Locks Notice that now the SPID of the second connection holds locks on various objects. 15. In SQL Server Management Studio, in the console tree, expand, expand Management, right-click Activity Monitor, and then click View Processes. 16. Review the information that is now displayed for the new SPID in Process Info, Locks by Process, and Locks by Object. 17. Close Activity Monitor. 18. Switch to the Lock1.sql query window, type the following code at the end of the script, and then execute the statement. ROLLBACK TRANSACTION 19. Switch back to the original query window, and then click Execute to execute the query and retrieve data from the sys.dm_tran_locks view. Notice that the object locks acquired during the transaction have now been released. 20. Close SQL Server Management Studio without saving the changes to the files. Task 2: Set locking options 1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then click SQL Server Management Studio. 2. In the Connect to Server dialog box, specify the values in the following table, and then click Connect. Server type Database Engine 3. On the File menu, point to Open, and then click File. Windows 4. In the Open File dialog box, browse to the D:\Labfiles\Starter folder, click Lock1.sql, 5. In the Connect to Database Engine dialog box, specify the values in the following Windows 6. Review the contents of the file, and then execute the query. 7. On the File menu, point to Open, and then click File. 8. In the Open File dialog box, browse to the D:\Labfiles\Starter folder, click Lock2.sql,
Lab Answer Key for Module 11: Managing Transactions and Locks 5 9. In the Connect to Database Engine dialog box, specify the values in the following Windows 10. Review the contents of the file and notice that the transaction isolation level is set to SERIALIZABLE, which prevents other users from accessing rows that match the criteria in a WHERE clause. Note: In the next step, you will execute the query. Continue with the exercise and do not wait for the execution to complete. 11. Click Execute to execute the query. 12. On the File menu, point to Open, and then click File. 13. In the Open File dialog box, browse to the D:\Labfiles\Starter folder, click LockList.sql, 14. In the Connect to Database Engine dialog box, specify the values in the following Windows 15. Click Execute to execute the query and notice that there is an entry of WAIT in the request_status column, which indicates that the second query is waiting for a lock to be granted before proceeding. 16. Switch to the Lock2.sql query window, and then click Cancel Executing Query. 17. Switch to the LockList.sql query window, and then click Execute to execute the query. 18. Verify that the waiting transaction has been cancelled. Task 3: Set the lock timeout 1. Switch to the Lock2.sql query window, and then edit the file by adding the following statement immediately before the BEGIN TRANSACTION statement. SET lock_timeout 5000 2. Click Execute to execute the query and wait five seconds. Notice that the query no longer waits indefinitely for the lock to be granted. 3. Close SQL Server Management Studio without saving the changes to the files.