The following describes the types of page and row locks:
Shared locks
Adaptive Server applies shared locks for read operations. If a shared lock has been applied to a data page or data row or to an index page, other transactions can also acquire a shared lock, even when the first transaction is active. However, no transaction can acquire an exclusive lock on the page or row until all shared locks on the page or row are released. This means that many transactions can simultaneously read the page or row, but no transaction can change data on the page or row while a shared lock exists. Transactions that need an exclusive lock wait or “block” for the release of the shared locks before continuing.
By default, Adaptive Server releases shared locks after it finishes scanning the page or row. It does not hold shared locks until the statement is completed or until the end of the transaction unless requested to do so by the user. For more details on how shared locks are applied, see “Locking for select queries at isolation Level 1”.
Exclusive locks
Adaptive Server applies an exclusive lock for a data modification operation. When a transaction gets an exclusive lock, other transactions cannot acquire a lock of any kind on the page or row until the exclusive lock is released at the end of its transaction. The other transactions wait or “block” until the exclusive lock is released.
Update locks
Adaptive Server applies an update lock during the initial phase of an update, delete, or fetch (for cursors declared for update) operation while the page or row is being read. The update lock allows shared locks on the page or row, but does not allow other update or exclusive locks. Update locks help avoid deadlocks and lock contention. If the page or row needs to be changed, the update lock is promoted to an exclusive lock as soon as no other shared locks exist on the page or row.
In general, read operations acquire shared locks, and write operations acquire exclusive locks. For operations that delete or update data, Adaptive Server applies page-level or row-level exclusive and update locks only if the column used in the search argument is part of an index. If no index exists on any of the search arguments, Adaptive Server must acquire a table-level lock.
The examples in Table 2-2 show what kind of page or row locks Adaptive Server uses for basic SQL statements. For these examples, there is an index acct_number, but no index on balance.
Statement |
Allpages-Locked Table |
Datarows-Locked Table |
---|---|---|
select balance from account where acct_number = 25 |
Shared page lock |
Shared row lock |
insert account values (34, 500) |
Exclusive page lock on data page and exclusive page lock on leaf-level index page |
Exclusive row lock |
delete account where acct_number = 25 |
Update page locks followed by exclusive page locks on data pages and exclusive page locks on leaf-level index pages |
Update row locks followed by exclusive row locks on each affected row |
update account set balance = 0 where acct_number = 25 |
Update page lock on data page and exclusive page lock on data page |
Update row lock followed by exclusive row lock |