The LOCK option requests a lock for each selected row.
Syntax
<lock_option> ::=
WITH LOCK [(NOWAIT)] [EXCLUSIVE | OPTIMISTIC] [ISOLATION LEVEL <unsigned_integer>]
Explanation
EXCLUSIVE
An exclusive lock (see
transactions) is defined. As long as the locked row has not been updated or deleted, the exclusive lock can be cancelled using the UNLOCK statement.OPTIMISTIC
OPTIMISTIC defines an optimistic lock on rows. This lock only makes sense when it is used together with the isolation levels 0, 1, 10, and 15. An update operation of the current user on a row which has been locked by this user using an optimistic lock is only performed if this row has not been updated in the meantime by a concurrent transaction. If this row has been changed in the meantime by a concurrent transaction, the update operation of the current user is rejected. The optimistic lock is released in both cases. If the update operation was successful, an exclusive lock is set for this row. If the update operation was not successful, it should be repeated after reading the row again with or without optimistic lock. In this way, it can be ensured that the update is done to the current state and that no modifications made in the meantime are lost.
The request of an optimistic lock only collides with an exclusive lock. Concurrent transactions do not collide with an optimistic lock.
SHARE lock
If neither EXCLUSIVE nor OPTIMISTIC is specified, a SHARE lock on rows is thus defined. If a SHARE lock was set on a row, no concurrent transaction can modify this row.
ISOLATION LEVEL
The locks are set independently of the ISOLATION specification (
isolation_spec) of the CONNECT statement. The isolation level of the LOCK option can identify a higher or lower value than that in the CONNECT statement. The meaning of the various isolation levels is explained in the description of the CONNECT statement.If an isolation level is specified by the lock option, it is only valid for the duration of the SQL statement which contains the LOCK option specification. Afterwards, the isolation level that was specified in the CONNECT statement is applicable again. In the case of a
SELECT statement (named_select_statement), SELECT statement (select_statement), or an OPEN CURSOR statement, for which the result table is not actually physically generated, the specified isolation level is valid for this SQL statement and all FETCH statements that refer to the result table. The isolation level that was specified in the CONNECT statement is applicable for other SQL statements that were executed in the meantime.NOWAIT
If
(NOWAIT) is specified, the database system does not wait until another user has released a data object. Instead, it returns a message if a collision occurs. If there is no collision, the requested lock is set.If
(NOWAIT) is not specified and a collision occurs, the system waits for the locked data object to be released (but only as long as is specified by the installation parameter REQUEST_TIMEOUT).