<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="metadata-locking">
      </a>
      10.11.4 Metadata Locking
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045222708352">
  </a>
  <a class="indexterm" name="idm46045222707280">
  </a>
  <a class="indexterm" name="idm46045222705792">
  </a>
  <p>
   MySQL uses metadata locking to manage concurrent access to
        database objects and to ensure data consistency. Metadata
        locking applies not just to tables, but also to schemas, stored
        programs (procedures, functions, triggers, scheduled events),
        tablespaces, user locks acquired with the
   <a class="link" href="locking-functions.html#function_get-lock">
    <code class="literal">
     GET_LOCK()
    </code>
   </a>
   function (see
   <a class="xref" href="locking-functions.html" title="14.14 Locking Functions">
    Section 14.14, “Locking Functions”
   </a>
   ), and locks acquired with
        the locking service described in
   <a class="xref" href="locking-service.html" title="7.6.9.1 The Locking Service">
    Section 7.6.9.1, “The Locking Service”
   </a>
   .
  </p>
  <p>
   The Performance Schema
   <a class="link" href="performance-schema-metadata-locks-table.html" title="29.12.13.3 The metadata_locks Table">
    <code class="literal">
     metadata_locks
    </code>
   </a>
   table exposes
        metadata lock information, which can be useful for seeing which
        sessions hold locks, are blocked waiting for locks, and so
        forth. For details, see
   <a class="xref" href="performance-schema-metadata-locks-table.html" title="29.12.13.3 The metadata_locks Table">
    Section 29.12.13.3, “The metadata_locks Table”
   </a>
   .
  </p>
  <p>
   Metadata locking does involve some overhead, which increases as
        query volume increases. Metadata contention increases the more
        that multiple queries attempt to access the same objects.
  </p>
  <p>
   Metadata locking is not a replacement for the table definition
        cache, and its mutexes and locks differ from the
   <code class="literal">
    LOCK_open
   </code>
   mutex. The following discussion
        provides some information about how metadata locking works.
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <a class="xref" href="metadata-locking.html#metadata-lock-acquisition" title="Metadata Lock Acquisition">
       Metadata Lock Acquisition
      </a>
     </p>
    </li>
    <li class="listitem">
     <p>
      <a class="xref" href="metadata-locking.html#metadata-lock-release" title="Metadata Lock Release">
       Metadata Lock Release
      </a>
     </p>
    </li>
   </ul>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h4 class="title">
       <a name="metadata-lock-acquisition">
       </a>
       Metadata Lock Acquisition
      </h4>
     </div>
    </div>
   </div>
   <p>
    If there are multiple waiters for a given lock, the
          highest-priority lock request is satisfied first, with an
          exception related to the
    <a class="link" href="server-system-variables.html#sysvar_max_write_lock_count">
     <code class="literal">
      max_write_lock_count
     </code>
    </a>
    system
          variable. Write lock requests have higher priority than read
          lock requests. However, if
    <a class="link" href="server-system-variables.html#sysvar_max_write_lock_count">
     <code class="literal">
      max_write_lock_count
     </code>
    </a>
    is set
          to some low value (say, 10), read lock requests may be
          preferred over pending write lock requests if the read lock
          requests have already been passed over in favor of 10 write
          lock requests. Normally this behavior does not occur because
    <a class="link" href="server-system-variables.html#sysvar_max_write_lock_count">
     <code class="literal">
      max_write_lock_count
     </code>
    </a>
    by
          default has a very large value.
   </p>
   <p>
    Statements acquire metadata locks one by one, not
          simultaneously, and perform deadlock detection in the process.
   </p>
   <p>
    DML statements normally acquire locks in the order in which
          tables are mentioned in the statement.
   </p>
   <p>
    DDL statements,
    <a class="link" href="lock-tables.html" title="15.3.6 LOCK TABLES and UNLOCK TABLES Statements">
     <code class="literal">
      LOCK TABLES
     </code>
    </a>
    ,
          and other similar statements try to reduce the number of
          possible deadlocks between concurrent DDL statements by
          acquiring locks on explicitly named tables in name order.
          Locks might be acquired in a different order for implicitly
          used tables (such as tables in foreign key relationships that
          also must be locked).
   </p>
   <p>
    For example,
    <a class="link" href="rename-table.html" title="15.1.36 RENAME TABLE Statement">
     <code class="literal">
      RENAME TABLE
     </code>
    </a>
    is a
          DDL statement that acquires locks in name order:
   </p>
   <div class="itemizedlist">
    <ul class="itemizedlist" style="list-style-type: disc; ">
     <li class="listitem">
      <p>
       This
       <a class="link" href="rename-table.html" title="15.1.36 RENAME TABLE Statement">
        <code class="literal">
         RENAME TABLE
        </code>
       </a>
       statement
              renames
       <code class="literal">
        tbla
       </code>
       to something else, and
              renames
       <code class="literal">
        tblc
       </code>
       to
       <code class="literal">
        tbla
       </code>
       :
      </p>
      <div class="copytoclipboard-wrapper">
       <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa51532940"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">RENAME</span> <span class="token keyword">TABLE</span> tbla <span class="token keyword">TO</span> tbld<span class="token punctuation">,</span> tblc <span class="token keyword">TO</span> tbla<span class="token punctuation">;</span></code></pre>
      </div>
      <p>
       The statement acquires metadata locks, in order, on
       <code class="literal">
        tbla
       </code>
       ,
       <code class="literal">
        tblc
       </code>
       , and
       <code class="literal">
        tbld
       </code>
       (because
       <code class="literal">
        tbld
       </code>
       follows
       <code class="literal">
        tblc
       </code>
       in name order):
      </p>
     </li>
     <li class="listitem">
      <p>
       This slightly different statement also renames
       <code class="literal">
        tbla
       </code>
       to something else, and renames
       <code class="literal">
        tblc
       </code>
       to
       <code class="literal">
        tbla
       </code>
       :
      </p>
      <div class="copytoclipboard-wrapper">
       <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa63221648"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">RENAME</span> <span class="token keyword">TABLE</span> tbla <span class="token keyword">TO</span> tblb<span class="token punctuation">,</span> tblc <span class="token keyword">TO</span> tbla<span class="token punctuation">;</span></code></pre>
      </div>
      <p>
       In this case, the statement acquires metadata locks, in
              order, on
       <code class="literal">
        tbla
       </code>
       ,
       <code class="literal">
        tblb
       </code>
       , and
       <code class="literal">
        tblc
       </code>
       (because
       <code class="literal">
        tblb
       </code>
       precedes
       <code class="literal">
        tblc
       </code>
       in name order):
      </p>
     </li>
    </ul>
   </div>
   <p>
    Both statements acquire locks on
    <code class="literal">
     tbla
    </code>
    and
    <code class="literal">
     tblc
    </code>
    , in that order, but differ in whether
          the lock on the remaining table name is acquired before or
          after
    <code class="literal">
     tblc
    </code>
    .
   </p>
   <p>
    Metadata lock acquisition order can make a difference in
          operation outcome when multiple transactions execute
          concurrently, as the following example illustrates.
   </p>
   <p>
    Begin with two tables
    <code class="literal">
     x
    </code>
    and
    <code class="literal">
     x_new
    </code>
    that have identical structure. Three
          clients issue statements that involve these tables:
   </p>
   <p>
    Client 1:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa71695431"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">LOCK</span> <span class="token keyword">TABLE</span> x <span class="token keyword">WRITE</span><span class="token punctuation">,</span> x_new <span class="token keyword">WRITE</span><span class="token punctuation">;</span></code></pre>
   </div>
   <p>
    The statement requests and acquires write locks in name order
          on
    <code class="literal">
     x
    </code>
    and
    <code class="literal">
     x_new
    </code>
    .
   </p>
   <p>
    Client 2:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa25439219"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> x <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
   </div>
   <p>
    The statement requests and blocks waiting for a write lock on
    <code class="literal">
     x
    </code>
    .
   </p>
   <p>
    Client 3:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa99460770"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">RENAME</span> <span class="token keyword">TABLE</span> x <span class="token keyword">TO</span> x_old<span class="token punctuation">,</span> x_new <span class="token keyword">TO</span> x<span class="token punctuation">;</span></code></pre>
   </div>
   <p>
    The statement requests exclusive locks in name order on
    <code class="literal">
     x
    </code>
    ,
    <code class="literal">
     x_new
    </code>
    , and
    <code class="literal">
     x_old
    </code>
    , but blocks waiting for the lock on
    <code class="literal">
     x
    </code>
    .
   </p>
   <p>
    Client 1:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa90821867"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">UNLOCK</span> <span class="token keyword">TABLES</span><span class="token punctuation">;</span></code></pre>
   </div>
   <p>
    The statement releases the write locks on
    <code class="literal">
     x
    </code>
    and
    <code class="literal">
     x_new
    </code>
    . The exclusive lock request for
    <code class="literal">
     x
    </code>
    by Client 3 has higher priority than the
          write lock request by Client 2, so Client 3 acquires its lock
          on
    <code class="literal">
     x
    </code>
    , then also on
    <code class="literal">
     x_new
    </code>
    and
    <code class="literal">
     x_old
    </code>
    , performs the renaming, and
          releases its locks. Client 2 then acquires its lock on
    <code class="literal">
     x
    </code>
    , performs the insert, and releases its
          lock.
   </p>
   <p>
    Lock acquisition order results in the
    <a class="link" href="rename-table.html" title="15.1.36 RENAME TABLE Statement">
     <code class="literal">
      RENAME TABLE
     </code>
    </a>
    executing before
          the
    <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
     <code class="literal">
      INSERT
     </code>
    </a>
    . The
    <code class="literal">
     x
    </code>
    into which the insert occurs is the table
          that was named
    <code class="literal">
     x_new
    </code>
    when Client 2 issued
          the insert and was renamed to
    <code class="literal">
     x
    </code>
    by Client
          3:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa46326642"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> x<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> i    <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span>    1 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> x_old<span class="token punctuation">;</span>
<span class="token output">Empty set (0.01 sec)</span></code></pre>
   </div>
   <p>
    Now begin instead with tables named
    <code class="literal">
     x
    </code>
    and
    <code class="literal">
     new_x
    </code>
    that have identical structure. Again,
          three clients issue statements that involve these tables:
   </p>
   <p>
    Client 1:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa70394209"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">LOCK</span> <span class="token keyword">TABLE</span> x <span class="token keyword">WRITE</span><span class="token punctuation">,</span> new_x <span class="token keyword">WRITE</span><span class="token punctuation">;</span></code></pre>
   </div>
   <p>
    The statement requests and acquires write locks in name order
          on
    <code class="literal">
     new_x
    </code>
    and
    <code class="literal">
     x
    </code>
    .
   </p>
   <p>
    Client 2:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa12225078"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> x <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
   </div>
   <p>
    The statement requests and blocks waiting for a write lock on
    <code class="literal">
     x
    </code>
    .
   </p>
   <p>
    Client 3:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa92475680"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">RENAME</span> <span class="token keyword">TABLE</span> x <span class="token keyword">TO</span> old_x<span class="token punctuation">,</span> new_x <span class="token keyword">TO</span> x<span class="token punctuation">;</span></code></pre>
   </div>
   <p>
    The statement requests exclusive locks in name order on
    <code class="literal">
     new_x
    </code>
    ,
    <code class="literal">
     old_x
    </code>
    , and
    <code class="literal">
     x
    </code>
    , but blocks waiting for the lock on
    <code class="literal">
     new_x
    </code>
    .
   </p>
   <p>
    Client 1:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa60939175"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">UNLOCK</span> <span class="token keyword">TABLES</span><span class="token punctuation">;</span></code></pre>
   </div>
   <p>
    The statement releases the write locks on
    <code class="literal">
     x
    </code>
    and
    <code class="literal">
     new_x
    </code>
    . For
    <code class="literal">
     x
    </code>
    , the
          only pending request is by Client 2, so Client 2 acquires its
          lock, performs the insert, and releases the lock. For
    <code class="literal">
     new_x
    </code>
    , the only pending request is by
          Client 3, which is permitted to acquire that lock (and also
          the lock on
    <code class="literal">
     old_x
    </code>
    ). The rename operation
          still blocks for the lock on
    <code class="literal">
     x
    </code>
    until the
          Client 2 insert finishes and releases its lock. Then Client 3
          acquires the lock on
    <code class="literal">
     x
    </code>
    , performs the
          rename, and releases its lock.
   </p>
   <p>
    In this case, lock acquisition order results in the
    <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
     <code class="literal">
      INSERT
     </code>
    </a>
    executing before the
    <a class="link" href="rename-table.html" title="15.1.36 RENAME TABLE Statement">
     <code class="literal">
      RENAME TABLE
     </code>
    </a>
    . The
    <code class="literal">
     x
    </code>
    into which the insert occurs is the
          original
    <code class="literal">
     x
    </code>
    , now renamed to
    <code class="literal">
     old_x
    </code>
    by the rename operation:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa63511930"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> x<span class="token punctuation">;</span>
<span class="token output">Empty set (0.01 sec)</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> old_x<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> i    <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span>    1 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
   </div>
   <p>
    If order of lock acquisition in concurrent statements makes a
          difference to an application in operation outcome, as in the
          preceding example, you may be able to adjust the table names
          to affect the order of lock acquisition.
   </p>
   <a class="indexterm" name="idm46045222606912">
   </a>
   <p>
    Metadata locks are extended, as necessary, to tables related
          by a foreign key constraint to prevent conflicting DML and DDL
          operations from executing concurrently on the related tables.
          When updating a parent table, a metadata lock is taken on the
          child table while updating foreign key metadata. Foreign key
          metadata is owned by the child table.
   </p>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h4 class="title">
       <a name="metadata-lock-release">
       </a>
       Metadata Lock Release
      </h4>
     </div>
    </div>
   </div>
   <p>
    To ensure transaction serializability, the server must not
          permit one session to perform a data definition language (DDL)
          statement on a table that is used in an uncompleted explicitly
          or implicitly started transaction in another session. The
          server achieves this by acquiring metadata locks on tables
          used within a transaction and deferring release of those locks
          until the transaction ends. A metadata lock on a table
          prevents changes to the table's structure. This locking
          approach has the implication that a table that is being used
          by a transaction within one session cannot be used in DDL
          statements by other sessions until the transaction ends.
   </p>
   <p>
    This principle applies not only to transactional tables, but
          also to nontransactional tables. Suppose that a session begins
          a transaction that uses transactional table
    <code class="literal">
     t
    </code>
    and nontransactional table
    <code class="literal">
     nt
    </code>
    as follows:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa22891176"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">START</span> <span class="token keyword">TRANSACTION</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t<span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> nt<span class="token punctuation">;</span></code></pre>
   </div>
   <p>
    The server holds metadata locks on both
    <code class="literal">
     t
    </code>
    and
    <code class="literal">
     nt
    </code>
    until the transaction ends. If
          another session attempts a DDL or write lock operation on
          either table, it blocks until metadata lock release at
          transaction end. For example, a second session blocks if it
          attempts any of these operations:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa31348119"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">DROP</span> <span class="token keyword">TABLE</span> t<span class="token punctuation">;</span>
<span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> t <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">;</span>
<span class="token keyword">DROP</span> <span class="token keyword">TABLE</span> nt<span class="token punctuation">;</span>
<span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> nt <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">;</span>
<span class="token keyword">LOCK</span> <span class="token keyword">TABLE</span> t <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token keyword">WRITE</span><span class="token punctuation">;</span></code></pre>
   </div>
   <p>
    The same behavior applies for The
    <a class="link" href="lock-tables.html" title="15.3.6 LOCK TABLES and UNLOCK TABLES Statements">
     <code class="literal">
      LOCK TABLES ...
          READ
     </code>
    </a>
    . That is, explicitly or implicitly started
          transactions that update any table (transactional or
          nontransactional) block and are blocked by
    <code class="literal">
     LOCK
          TABLES ... READ
    </code>
    for that table.
   </p>
   <p>
    If the server acquires metadata locks for a statement that is
          syntactically valid but fails during execution, it does not
          release the locks early. Lock release is still deferred to the
          end of the transaction because the failed statement is written
          to the binary log and the locks protect log consistency.
   </p>
   <p>
    In autocommit mode, each statement is in effect a complete
          transaction, so metadata locks acquired for the statement are
          held only to the end of the statement.
   </p>
   <p>
    Metadata locks acquired during a
    <a class="link" href="prepare.html" title="15.5.1 PREPARE Statement">
     <code class="literal">
      PREPARE
     </code>
    </a>
    statement are released
          once the statement has been prepared, even if preparation
          occurs within a multiple-statement transaction.
   </p>
   <p>
    For XA transactions in
    <code class="literal">
     PREPARED
    </code>
    state,
          metadata locks are maintained across client disconnects and
          server restarts, until an
    <a class="link" href="xa.html" title="15.3.8 XA Transactions">
     <code class="literal">
      XA COMMIT
     </code>
    </a>
    or
    <a class="link" href="xa.html" title="15.3.8 XA Transactions">
     <code class="literal">
      XA ROLLBACK
     </code>
    </a>
    is
          executed.
   </p>
  </div>
 </div>
 <br/>
</div>
