Oracle 5.0 Reference Manual page 1289

Table of Contents

Advertisement

• First update the counter by incrementing it by 1, and then read it.
• First perform a locking read of the counter using
The latter approach can be implemented as follows:
SELECT counter_field FROM child_codes FOR UPDATE;
UPDATE child_codes SET counter_field = counter_field + 1;
A
SELECT ... FOR UPDATE
reads. Thus, it sets the same locks a searched SQL
The preceding description is merely an example of how
the specific task of generating a unique identifier actually can be accomplished using only a single
access to the table:
UPDATE child_codes SET counter_field = LAST_INSERT_ID(counter_field + 1);
SELECT LAST_INSERT_ID();
The
SELECT
does not access any table.
14.2.7.4.
Record, Gap, and Next-Key Locks
InnoDB
has several types of record-level locks:
InnoDB
• Record lock: This is a lock on an index record.
• Gap lock: This is a lock on a gap between index records, or a lock on the gap before the first or after
the last index record.
• Next-key lock: This is a combination of a record lock on the index record and a gap lock on the gap
before the index record.
Record locks always lock index records, even if a table is defined with no indexes. For such cases,
creates a hidden clustered index and uses this index for record locking. See
InnoDB
"Clustered and Secondary
By default,
InnoDB
innodb_locks_unsafe_for_binlog
next-key locks for searches and index scans, which prevents phantom rows (see
"Avoiding the Phantom Problem Using Next-Key
Next-key locking combines index-row locking with gap locking.
such a way that when it searches or scans a table index, it sets shared or exclusive locks on the index
records it encounters. Thus, the row-level locks are actually index-record locks. In addition, a next-key
lock on an index record also affects the "gap" before that index record. That is, a next-key lock is an
index-record lock plus a gap lock on the gap preceding the index record. If one session has a shared
or exclusive lock on record
immediately before
Suppose that an index contains the values 10, 11, 13, and 20. The possible next-key locks for this
index cover the following intervals, where
denote inclusion of the endpoint:
The
InnoDB
reads the latest available data, setting exclusive locks on each row it
statement merely retrieves the identifier information (specific to the current connection). It
Note
Locking of rows for update using
when autocommit is disabled (either by beginning transaction with
or by setting
TRANSACTION
the rows matching the specification are not locked.
Indexes".
operates in
REPEATABLE READ
in an index, another session cannot insert a new index record in the gap
R
in the index order.
R
Transaction Model and Locking
UPDATE, and then increment the counter.
FOR
UPDATE
SELECT ... FOR UPDATE
SELECT FOR UPDATE
autocommit
[1120]
transaction isolation level and with the
[1244]
system variable disabled. In this case,
Locking").
or
denote exclusion of the interval endpoint and
(
)
1269
would set on the rows.
only applies
[436]
to 0. If autocommit is enabled,
Section 14.2.7.5,
performs row-level locking in
InnoDB
works. In MySQL,
START
Section 14.2.9.1,
uses
InnoDB
or
[
]

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents