Specifying Locking Hints in a SQL Server Database


You need to pessimistically lock rows in an underlying SQL Server database.


Use SQL Server locking hints from ADO.NET.

The sample code contains three event handlers:

Start Tran Button.Click

Creates a SQL SELECT statement to retrieve the Orders table from the Northwind database. A locking hint, either UPDLOCK or HOLDLOCK , is added to the statement as specified. A Connection is opened and a Transaction started on it with an isolation level of ReadCommitted . A DataAdapter is used on the transacted connection to fill a DataTable . A CommandBuilder is created to generate updating logic. The default view of the table is bound to the data grid on the form.

Cancel Button.Click

Clears the data grid, rolls back the transaction, and closes the connection.


Rolls back the transaction if it exists and closes the connection.

The C# code is shown in Example 6-39.

Example 6-39. File: UsingLockingHintsForPessimisticLockingForm.cs

// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

private SqlConnection conn;
private SqlTransaction tran;

// . . . 

private void startButton_Click(object sender, System.EventArgs e)
 startButton.Enabled = false;
 String sqlText = "SELECT * FROM Orders WITH ";

 // Add pessimistic locking as specified by user.
 sqlText += "(UPDLOCK)";
 else if(holdLockRadioButton.Checked)
 sqlText += "(HOLDLOCK)";

 // Create connection.
 conn = new SqlConnection(
 conn.Open( );
 // Start the transaction.
 tran = conn.BeginTransaction(IsolationLevel.ReadCommitted);
 // Create the command.
 SqlCommand cmd = new SqlCommand(sqlText, conn, tran);
 // Create the DataAdapter and CommandBuilder.
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 SqlCommandBuilder cb = new SqlCommandBuilder(da);
 // Fill table using the DataAdapter.
 DataTable dt = new DataTable( );

 // Bind the default view of the table to the grid.
 dataGrid.DataSource = dt.DefaultView;
 cancelButton.Enabled = true;
 dataGrid.ReadOnly = false;

private void cancelButton_Click(object sender, System.EventArgs e)
 cancelButton.Enabled = false;

 // Unbind the table from the grid.
 dataGrid.DataSource = null;

 // Roll back the transaction and close the connection.
 tran.Rollback( );
 conn.Close( );

 startButton.Enabled = true;


A lock is an object indicating that a user has a dependency on a resource. Locks ensure transactional integrity and database consistency by preventing other users from changing data being read by a user and preventing users from reading data being changed by a user. Locks are acquired and released by user actions; they are managed internally by database software.

A locking hint can be specified with SELECT , INSERT , DELETE , and UPDATE statements to instruct SQL Server as to the type of lock to use. You can use locking hints when you need control over locks acquired on objects. The SQL Server Optimizer automatically determines correct locking; hints should be used only when necessary. Locking hints override the current transaction isolation level for the session.

A locking hint is specified following the FROM clause using a WITH clause. The hint is specified within parentheses and multiple hints are separated by commas.

Tables Table 6-21, Table 6-22, and Table 6-23 describe the different locking hints that you can use, categorized according to their function.

Table 6-21. SQL Server locking hints for isolation level

Locking hint



Hold a shared lock until the transaction is completed instead of releasing it as soon as the required objecttable, row, or data pageis no longer needed.


Do not issue shared locks and do not recognize exclusive locks. Applies only to the SELECT statement.


Use the same locking as a transaction with an isolation level of READ COMMITTED .


Same as NOLOCK .


Use the same locking as a transaction with an isolation level of REPEATABLE READ .


Use the same locking as a transaction with an isolation level of SERIALIZABLE .

Table 6-22. SQL Server locking hints for granularity

Locking hint



Do not issue shared locks and do not recognize exclusive locks. Applies only to the SELECT statement.


Use page locks where a single table lock would normally be used.


Use row-level locking instead of page-level and table-level locking.


Use table-level locking instead of row-level and page-level locking. By default, the lock is held until the end of the statement.


Use an exclusive table lock preventing other users from reading or updating the table. By default, the lock is held until the end of the statement.

Table 6-23. SQL Server Locking Hints for Other Functions

Locking hint



Skip locked rows that would ordinarily appear in the result set rather than blocking the transaction by waiting for other transactions to release locks on those rows. Applies only to transactions with an isolation level of READ COMMITTED . Applies only to the SELECT statement.


Use update locks instead of shared locks when reading a table. This allows you to read data and later update it with a guarantee that it has not changed since you last read it while other users are not blocked from reading the data. Cannot be used with NOLOCK or XLOCK .


Use an exclusive lock that is held until the end of the transaction on all data processed by the statement. Can be specified with either PAGLOCK or TABLOCK granularity. Cannot be used with either NOLOCK or UPDLOCK .

There are a number ways to get information about database locks:

  • The system stored procedure sp_lock returns a result set containing all active locks.
  • The syslockinfo table in the master database contains information about all granted, converting, and waiting lock requests . It is a denormalized view of the data structures used internally by the lock manager.
  • The SQL Server Profiler can be used to monitor and record locking information.
  • The Windows Performance Monitor has a SQL Server Locks Object counter that can be used to monitor lock activity.

For more information about database locks, using locking hints, or monitoring database locks, see Microsoft SQL Server Books Online.

Binding Data to NET User Interfaces

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax

ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net