INF: Locking Behavior of Cursors on SQL Server Version 6.0

ID: Q132037


The information in this article applies to:


SUMMARY

Microsoft SQL Server version 6.0 has implemented server cursors and a user controllable setting for TRANSACTION ISOLATION LEVEL. When used together, it can be useful to know the expected locking behavior.


MORE INFORMATION

This document attempts to clarify the effect of optimizer hints and isolation levels on the locking considerations of cursors. The nonlocking parts of optimizer hints affect the cursor behavior in the following ways:

The following page gives a detailed table of the kinds of locks taken with various hints and isolation levels. Some of the shorthand terminology is explained below:

RDL - Read locks: These are shared page locks that are taken while the pages are being read and released immediately after the page read is complete.

SCR -Scroll locks: These locks are taken by the cursor fetch command on pages that the rows are returned from. These locks will remain in place as long as the last fetch command has rows fetched from these pages; and released either when a succeeding fetch scrolls off these pages or the cursor is closed. These locks come in two varieties: shared SCR(sh), and update SCR(up) page locks. Only the locks on the data pages will be kept, no locks on the index pages will be taken. Furthermore, if SCROLL locking is in effect on the cursor (shared or update) and there is an outstanding transaction in effect during opening of a keyset based cursor, shared locks will be accumulated on pages generating the keyset and these locks will be kept until the transaction ends.

XT/C Locks: These page locks are taken by the cursor fetch command on pages that the rows are returned from. If a transaction is outstanding, these locks will be kept within the duration of the transaction and released when the transaction commits or rolls back. If there are no transactions, these locks will be kept as long as the cursor is open, or a new transaction is started and then ended. These locks also have shared and update varieties. XT/C(sh) and XT/C(up) for page locks

XT(tb) and XT(tbx): Shared and exclusive table locks. These locks can only be taken inside a transaction and can only live within the life of the transaction, not the cursor.

The following table attempts to clarify in detail what kind of locks will be taken with cursors under different locking hints and optimizer levels:

TABLE OPTIMIZER HINT

Cursor       Isolation
Concurr.      Level   No hints    NOLOCK    HOLDLOCK   UPDLOCK
Option
___________________________________________________________________
OPTCC       Uncomm.
OPTVAL        (0)       Disallowed Disallowed Disallowed Disallowed
   -----------------------------------------------------------
Comtd.
              (1)       RDL         NONE      SCR(sh)    SCR(up)
   -----------------------------------------------------------
            Serial/rpt
              (2)       SCR(sh)     NONE      SCR(sh)    SCR(up)
                        XT/C(sh)              XT/C(sh)   XT/C(up)
__________________________________________________________________
LOCKCC      Uncomm.
              (0)       Disallowed Disallowed Disallowed Disallowed
-------------------------------------------------------------------
            Comtd.
              (1)       SCR(up)    NONE       SCR(up)    SCR(up)
-------------------------------------------------------------------
            Serial/rpt
              (2)       SCR(up)    NONE       SCR(up)    SCR(up)
                        XT/C(up)              XT/C(up)   XT/C(up)
___________________________________________________________________
READ        Uncomm.
ONLY          (0)       NONE       NONE       SCR(sh)    Disallowed
            Comtd.
              (1)       RDL        NONE       SCR(sh)    Disallowed
-------------------------------------------------------------------
            Serial/rpt
              (2)       SCR(sh)    NONE       SCR(sh)    Disallowed
                        XT/C(sh)              XT/C(sh)
___________________________________________________________________
Part 2
TABLE OPTIMIZER HINT

Cursor       Isolation
Concurr.     Level        TABLOCK          TABLOCKX
Option
___________________________________________________________
OPTCC        Uncomm.
OPTVAL        (0)         Disallowed      Disallowed
             Comtd.     (inside xact only) (inside xact only)
              (1)         XT(tb)            XT(tbx)
------------------------------------------------------------
             Serial/rpt
              (2)       (inside xact only) (inside xact only)
                          XT(tb)            XT(tbx)
____________________________________________________________
LOCKCC       Uncomm.
              (0)         Disallowed        Disallowed
------------------------------------------------------------
             Comtd.     (inside xact only) (inside xact only)
              (1)         XT(tb)            XT(tbx)
------------------------------------------------------------
             Serial/rpt
              (2)       (inside xact only) (inside xact only)
                          XT(tb)            XT(tbx)
____________________________________________________________
READ         Uncomm.
ONLY          (0)        NONE               Disallowed
             Comtd.
              (1)        NONE               Disallowed
------------------------------------------------------------
             Serial/rpt
              (2)       (inside xact only)  Disallowed
                          XT(tb)
_______________________________________________________

NOTE: The above two tables should also fit side by side into one table. 

Additional query words: Windows NT sql6 cursor lock concurrency


Keywords          : kbusage SSrvWinNT 
Version           : 6.0
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: April 16, 1999