Alexander Chigrikchigrik@hotmail.com
In this article I want to tell you about general details of SQL Server 6.5 locking, about Transaction Isolation Level, what kind of Transaction Isolation Level exists, and how you can set the appropriate Transaction Isolation Level, about Lock types and Locking optimizer hints, about Lock Escalation, about Deadlocks, about how you can view locks with sp_lock stored procedure, and how you can set insert row lock for a particular table or tables.
There are three units of data storage in SQL Server 6.5:
There are four isolation levels:
SET TRANSACTION ISOLATION LEVEL { READ COMMITTED | READ UNCOMMITTED | REPEATABLE READ | SERIALIZABLE }
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED GO DBCC USEROPTIONS GO
Set Option Value ------------------------------ ------------------------------------ textsize 64512 language us_english dateformat mdy datefirst 7 isolation level read uncommitted
There are three main types of locks that SQL Server 6.5 uses:
There are six Locking optimizer hints in SQL Server 6.5:
SELECT au_fname FROM pubs..authors (holdlock)
You can customize locking by setting Lock Escalation level. The Lock Escalation level determines, when SQL Server applies table locks instead of page locks, and it affects all users of SQL Server. So it's escalation from the page's to the table's level locking. There are three Lock Escalation options:
EXEC sp_configure 'LE threshold maximum' GO EXEC sp_configure 'LE threshold maximum', 250 GO RECONFIGURE WITH OVERRIDE GO EXEC sp_configure 'LE threshold maximum' GO
name minimum maximum config_value run_value ----------------------------------- ----------- ----------- ------------ ----------- LE threshold maximum 2 500000 200 200 Configuration option changed. Run the RECONFIGURE command to install. name minimum maximum config_value run_value ----------------------------------- ----------- ----------- ------------ ----------- LE threshold maximum 2 500000 250 250
Deadlock occurs when two users have locks on separate objects and each user wants a lock on the other's object. For example, User1 has a lock on object "A" and wants a lock on object "B" and User2 has a lock on object "B" and wants a lock on object "A". In this case, SQL Server 6.5 ends a deadlock by choosing the user, who will be a deadlock victim. After that, SQL Server rolls back the breaking user's transaction, sends message number 1205 to notify the user's application about breaking, and then allows the nonbreaking user's process to continue. You can decide which connection will be the candidate for deadlock victim by using SET DEADLOCK_PRIORITY. In other case, SQL Server selects the deadlock victim by choosing the process that completes the circular chain of locks. So, in a multiuser situation, your application should check for message 1205 to indicate that the transaction was rolled back and if so, to restart the transaction. Note To reduce the chance of a deadlock, you should minimize the size of transactions and transaction times.
Sometimes you need a reference to information about locks. Microsoft recommends to use sp_lock system stored procedure to report locks information. This very useful procedure returns the information about SQL Server process ID, which lock the data, about locked database, about locked table ID, about locked page and about type of locking (locktype column). This is the results set of sp_lock stored procedure: spid locktype table_id page dbname ------ ----------------------------------- ----------- ----------- --------------- 11 Sh_intent 688005482 0 master 11 Ex_extent 0 336 tempdb
spid locktype table_id page dbname ------ ----------------------------------- ----------- ----------- --------------- 11 Sh_intent 688005482 0 master 11 Ex_extent 0 336 tempdb
You can set insert row lock option for the user-defined table/tables with sp_tableoption system stored procedure. This is the syntax from SQL Server Books Online: sp_tableoption @TableNamePattern [, '@OptionName'] [, '@OptionValue'] where @TableNamePattern is a user-defined database table. @OptionName is a option name. There are insert row lock and pintable options. @OptionValue is a option value. @OptionValue can be 'false' or 'true'. The default value for insert row lock option is 'false'. This is the example to set insert row lock option for authors table: EXEC sp_tableoption 'authors', 'insert row lock', 'true'
sp_tableoption @TableNamePattern [, '@OptionName'] [, '@OptionValue']
EXEC sp_tableoption 'authors', 'insert row lock', 'true'
1. SQL Server Books Online 2. Transaction Isolation Level 3. SQL Server 6.5: Locking 4. Detailed locking view: sp_lock2 5. INF: Analyzing and Avoiding Deadlocks in SQL Server http://support.microsoft.com/support/kb/articles/Q169/9/60.ASP
To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.
We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:
We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.
Steve Jones Editor, SQLServerCentral.com