LOCK TABLES
and
UNLOCK TABLES
Syntax
1117
This policy ensures that table locking is deadlock free. There are, however, other things you need to
be aware of about this policy: If you are using a
LOW_PRIORITY WRITE
lock for a table, it means only
that MySQL waits for this particular lock until there are no other sessions that want a
READ
lock. When
the session has gotten the
WRITE
lock and is waiting to get the lock for the next table in the lock table
list, all other sessions wait for the
WRITE
lock to be released. If this becomes a serious problem with
your application, you should consider converting some of your tables to transaction-safe tables.
Rules for Lock Release
When the table locks held by a session are released, they are all released at the same time. A session
can release its locks explicitly, or locks may be released implicitly under certain conditions.
• A session can release its locks explicitly with
UNLOCK TABLES
.
• If a session issues a
LOCK TABLES
statement to acquire a lock while already holding locks, its
existing locks are released implicitly before the new locks are granted.
• If a session begins a transaction (for example, with
START TRANSACTION
), an implicit
UNLOCK
TABLES
is performed, which causes existing locks to be released. (For additional information about
the interaction between table locking and transactions, see
Section 13.3.5.1, “Interaction of Table
Locking and Transactions”
.)
If the connection for a client session terminates, whether normally or abnormally, the server implicitly
releases all table locks held by the session (transactional and nontransactional). If the client
reconnects, the locks will no longer be in effect. In addition, if the client had an active transaction, the
server rolls back the transaction upon disconnect, and if reconnect occurs, the new session begins with
autocommit enabled. For this reason, clients may wish to disable auto-reconnect. With auto-reconnect
in effect, the client is not notified if reconnect occurs but any table locks or current transaction will have
been lost. With auto-reconnect disabled, if the connection drops, an error occurs for the next statement
issued. The client can detect the error and take appropriate action such as reacquiring the locks or
redoing the transaction. See
Section 20.6.14, “Controlling Automatic Reconnection Behavior”
.
Note
If you use
ALTER TABLE
on a locked table, it may become unlocked. For
example, if you attempt a second
ALTER TABLE
operation, the result may be
an error
Table 'tbl_name' was not locked with LOCK TABLES
.
To handle this, lock the table again prior to the second alteration. See also
Section C.5.7.1, “Problems with
ALTER TABLE
”
.
13.3.5.1. Interaction of Table Locking and Transactions
LOCK TABLES
and
UNLOCK TABLES
interact with the use of transactions as follows:
•
LOCK TABLES
is not transaction-safe and implicitly commits any active transaction before attempting
to lock the tables.
•
UNLOCK TABLES
implicitly commits any active transaction, but only if
LOCK TABLES
has been used
to acquire table locks. For example, in the following set of statements,
UNLOCK TABLES
releases the
global read lock but does not commit the transaction because no table locks are in effect:
FLUSH TABLES WITH READ LOCK;
START TRANSACTION;
SELECT ... ;
UNLOCK TABLES;
• Beginning a transaction (for example, with
START TRANSACTION
) implicitly commits any current
transaction and releases existing table locks.
•
FLUSH TABLES WITH READ LOCK
acquires a global read lock and not table locks, so it is not
subject to the same behavior as
LOCK TABLES
and
UNLOCK TABLES
with respect to table locking
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...