Previous | Table of Contents | Next |
The sp_lock stored procedure will display the locks currently held by all processes. If you provide a process ID to the procedure, it will return all locks held by that process.
To create some locks, you begin a procedure, update a row in titles, and run the sp_lock stored procedure:
begin tran update titles set title = This change will be rolled back. where title_id = BU1111 exec sp_lock
(1 row(s) affected) spid locktype table_id page dbname ---- ----------------- --------- ------ --------- 10 Sh_intent 688005482 0 master 10 Ex_intent 192003715 0 pubs 10 Ex_page 192003715 400 pubs 10 Ex_page 192003715 688 pubs 10 Update_page 192003715 400 pubs 10 Ex_extent 0 368 tempdb
sp_lock output is composed of rows from the master..syslocks table. This table tracks locks on tables and pages for all databases.
The first column, spid, is the process ID that holds the lock. The spid is used internally by the server to track processes and things related to processes, such as CPU time used, when they were connected, locks, and so on.
Locktype indicates the type of lock that has been acquired. The character before the underscore indicate the base type. There are three base types (shared, exclusive, and update), described in the next section. The second half of the locktype is the granularity of the lock. Lock granularity is the size of the object affected by the lock. It could be a page (one 2KB page of data), a row (if row-level locking is enabled), a table, or one of two special lock types, an extent or an intent lock. This is all discussed later in this chapter in the section Lock Granularity: Table Locking, Page Locking, and Row Locking.
The table_id column indicates the ID number of the table on which the lock has been acquired. You can get the name of the object by changing to the appropriate database and executing this query:
select object_name(<Insert ID here>) select object_name(192003715)
---------------------------------- titles
Object IDs are allocated when an object is created. It is very unlikely that your object IDs in pubs will match mine. Object IDs for system tables in master always match, because the process for creating them is identical for all SQL Server installations.
The page column tells which page number is locked. This information is useful to determine where, exactly, in a table contention is occurring. I wont go into that too much, here, though. Intent, extent, and table locks, which operate on objects larger than pages, show zero in the page column.
Finally, the dbname column describes the database in which the lock is held.
Ill talk a bit about all the different lock types you will see, and then Ill come back to this output to talk about the locks you see here.
There are three basic types of locks available:
The following list describes some of the most interesting implications of these different locks:
For these reasons, on a system that needs to handle lots of activity (these are called high throughput systems), you shouldnt keep transactions open in an interactive session, as Ive been doing in the transaction examples here.
WARNING: Never, ever, under any circumstances, should you hold locks open pending user input. This is an invitation for disaster. The great programming blooper example is beginning a transaction, making some changes, and then showing a Windows message box asking, Commit changes? Inevitably your user will go to lunch and bring the database to a screeching halt for hours. Or if the user is hit by the proverbial bus (you know, the one that always comes up when your boss talks about how youre the only one who knows the details of a program), the database holds locks until the users process is killed. At least it lived longer than the user.Exclusive locks keep other users waiting until those locks are released. If you need to perform transactions in your programs, which is usually the case, gather all the information from your user, request verification, and then submit all the changes inside a transaction.
If you must read rows, allow user interaction, and submit changes, consider using optimistic locking. This is discussed on Day 20.
Previous | Table of Contents | Next |