LOCK Statement(lock_statement)
The LOCK statement assigns a lock to the current
transaction (see lock).Syntax
<lock_statement> ::=
LOCK [(WAIT)|(NOWAIT)] <lock_spec> IN SHARE MODE
LOCK [(WAIT)|(NOWAIT)] <lock_spec> IN EXCLUSIVE MODE
LOCK [(WAIT)|(NOWAIT)] <lock_spec> IN SHARE MODE <lock_spec> IN EXCLUSIVE MODE
LOCK [(WAIT)|(NOWAIT)] <row_spec>... OPTIMISTIC
<lock_spec> ::= TABLE <table_name>,... | <row_spec>...
| TABLE <table_name>,... <row_spec>...
Explanation
The specified table cannot be a base table, a view table (see
table), nor a synonym. If the table name identifies a view table, locks are set on the base tables on which the view table is based.WAIT/NOWAIT
<row_spec>...
A
<row_spec>... creates a lock for the table row denoted by the key values or a position in a result table.Specifying a
row_spec requires that the specified table have a key column; that is, if the table name identifies a view table, this must be modifiable.TABLE <table_name>,...
If
TABLE <table_name>,... is specified, a lock is created for the table in question.If the view table identified by the table name is not changeable, only a
share lock can be set for this view table. As a result of this SQL statement, share locks are set for all base tables underlying the view table.SHARE
SHARE defines a
share lock for the listed objects. To set share locks, the current user must have the SELECT privilege.EXCLUSIVE
EXCLUSIVE defines an
exclusive lock for the listed objects. To set exclusive locks, the current user must have the UPDATE, DELETE, or INSERT privilege.OPTIMISTIC
OPTIMISTIC defines an
optimistic lock on rows. This is only meaningful in connection with isolation levels 0, 1, 10, and 15.Deadlock
Whenever the database system recognizes a deadlock caused by locks, it ends the transaction with an implicit ROLLBACK WORK.
Reproducibility
If reproducible results are needed to read rows using a select statement, the read objects must be locked and the locks must be kept until reproduction. Reproducibility usually requires that share locks are set for the tables concerned, either by explicitly locking them using one or more lock statements, or implicitly by using the
isolation level 3. This ensures that other users cannot modify the table.See also:
Lock Behavior