<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->

<refentry xml:id="function.mysqlnd-ms-xa-begin" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>mysqlnd_ms_xa_begin</refname>
  <refpurpose>Starts a distributed/XA transaction among MySQL servers</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>int</type>
   <methodname>mysqlnd_ms_xa_begin</methodname>
   <methodparam><type>mixed</type><parameter>connection</parameter></methodparam>
   <methodparam><type>string</type><parameter>gtrid</parameter></methodparam>
   <methodparam choice="opt"><type>int</type><parameter>timeout</parameter></methodparam>
  </methodsynopsis>
  <para>
   Starts a XA transaction among MySQL servers. PECL/mysqlnd_ms acts as a transaction
   coordinator the distributed transaction.
  </para>
  <para>
   Once a global transaction has been started, the plugin injects appropriate
   <literal>XA BEGIN</literal> SQL statements on all MySQL servers used in the following.
   The global transaction is either ended by calling <function>mysqlnd_ms_xa_commit</function>,
   <function>mysqlnd_ms_xa_rollback</function> or by an implicit rollback in case
   of an error.
  </para>
  <para>
   During a global transaction, the plugin tracks all server switches,
   for example, when switching from one MySQL shard to another MySQL shard.
   Immediately before a query is run on a server that has not been participating
   in the global transaction yet, <literal>XA BEGIN</literal> is executed on
   the server. From a users perspective the injection happens during a call to a
   query execution function such as  <function>mysqli_query</function>. Should
   the injection fail an error is reported to the caller of the query execution
   function. The failing server does not become a participant in the global
   transaction. The user may retry executing a query on the server and hereby retry
   injecting <literal>XA BEGIN</literal>, abort the global transaction because
   not all required servers can participate, or ignore and continue the global without
   the failed server.
  </para>
  <para>
   Reasons to fail executing <literal>XA BEGIN</literal> include but are not limited to
   a server being unreachable or the server having an open, concurrent
   XA transaction using the same xid.
  </para>
  <para>
   Please note, global and local transactions
   are mutually exclusive. You cannot start a XA transaction when you have a local
   transaction open. The local transaction must be ended first. The plugin tries
   to detect this conflict as early as possible. It monitors API calls for controlling
   local transactions to learn about the current state. However, if using
   SQL statements for local transactions such as <literal>BEGIN</literal>, the
   plugin may not know the current state and the conflict is not detected
   before <literal>XA BEGIN</literal> is injected and executed.
  </para>
  <para>
   The use of other XA resources but MySQL servers is not supported by
   the function. To carry out a global transaction among, for example,
   a MySQL server and another vendors database system, you should issue
   the systems SQL commands yourself.
  </para>
  <note>
   <title>Experimental</title>
   <para>
    The feature is currently under development. There may be issues and/or
    feature limitations. Do not use in production environments.
   </para>
  </note>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <variablelist>
    <varlistentry>
    <term><parameter>connection</parameter></term>
    <listitem>
     <para>
      A MySQL connection handle obtained from any of the
      connect functions of the <link linkend="ref.mysqli">mysqli</link>,
      <link linkend="ref.mysql">mysql</link> or
      <link linkend="ref.pdo-mysql">PDO_MYSQL</link> extensions.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term><parameter>gtrid</parameter></term>
    <listitem>
     <para>
      Global transaction identifier (gtrid). The gtrid is a binary string
      up to 64 bytes long. Please note, depending on your character set settings,
      64 characters may require more than 64 bytes to store.
     </para>
     <para>
     In accordance with the MySQL SQL syntax,
     XA transactions use identifiers made of three parts. An xid consists of a
     global transaction identifier (gtrid), a branch qualifier (bqual) and a
     format identifier (formatID). Only the global transaction identifier can
     and needs to be set.
     </para>
     <para>
     The branch qualifier and format identifier are set automatically. The details
     should be considered implementation dependent, which may change without
     prior notice. In version 1.6 the branch qualifier is consecutive number
     which is incremented whenever a participant joins the global transaction.
     </para>
    </listitem>
   </varlistentry>
    <varlistentry>
    <term><parameter>timeout</parameter></term>
    <listitem>
     <para>
      Timeout in seconds. The default value is 60 seconds.
     </para>
     <para>
      The timeout is a hint to the garbage collection. If a transaction is recorded
      to take longer than expected, the garbage collection begins checking the
      transactions status.
     </para>
     <para>
       Setting a low value may make the garbage collection check the progress
       too often. Please note, checking the status of a global transaction may
       involve connecting to all recorded participants and possibly issuing
       queries on the servers.
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
   <para>
   Returns &true; if there is no open local or global transaction and a new global
   transaction can be started. Otherwise, returns &false;
  </para>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member>
     <link linkend="mysqlnd-ms.quickstart.xa_transactions">Quickstart XA/Distributed transactions</link>
    </member>
    <member>
     <link linkend="mysqlnd-ms.configuration">Runtime configuration</link>
    </member>
    <member>
     <function>mysqlnd_ms_get_stats</function>
    </member>
   </simplelist>
  </para>
 </refsect1>

</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->