<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.oci-rollback" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>oci_rollback</refname>
  <refpurpose>Rolls back the outstanding database transaction</refpurpose>
 </refnamediv>
 
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>bool</type><methodname>oci_rollback</methodname>
   <methodparam><type>resource</type><parameter>connection</parameter></methodparam>
  </methodsynopsis>
  <para>
   Reverts all uncommitted changes for the Oracle
   <parameter>connection</parameter> and ends the transaction.  It
   releases all locks held.  All Oracle <literal>SAVEPOINTS</literal>
   are erased.
  </para>
  <para>
   A transaction begins when the first SQL statement that changes data
   is executed with <function>oci_execute</function> using
   the <constant>OCI_NO_AUTO_COMMIT</constant> flag.  Further data
   changes made by other statements become part of the same
   transaction.  Data changes made in a transaction are temporary
   until the transaction is committed or rolled back.  Other users of
   the database will not see the changes until they are committed.
  </para>
  <para>
   When inserting or updating data, using transactions is recommended
   for relational data consistency and for performance reasons.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>connection</parameter></term>
     <listitem>
      <para>
       An Oracle connection identifier, returned by
       <function>oci_connect</function>, <function>oci_pconnect</function>
       or <function>oci_new_connect</function>.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   &return.success;
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title><function>oci_rollback</function> example</title>
    <programlisting role="php">
<![CDATA[
<?php

// Insert into several tables, rolling back the changes if an error occurs

$conn = oci_connect('hr', 'welcome', 'localhost/XE');

$stid = oci_parse($conn, "INSERT INTO mysalary (id, name) VALUES (1, 'Chris')");

// The OCI_NO_AUTO_COMMIT flag tells Oracle not to commit the INSERT immediately
$r = oci_execute($stid, OCI_NO_AUTO_COMMIT);
if (!$r) {    
    $e = oci_error($stid);
    trigger_error(htmlentities($e['message']), E_USER_ERROR);
}

$stid = oci_parse($conn, 'INSERT INTO myschedule (startday) VALUES (12)');
$r = oci_execute($stid, OCI_NO_AUTO_COMMIT);
if (!$r) {    
    $e = oci_error($stid);
    oci_rollback($conn);  // rollback changes to both tables
    trigger_error(htmlentities($e['message']), E_USER_ERROR);
}

// Commit the changes to both tables
$r = oci_commit($conn);
if (!r) {
    $e = oci_error($conn);
    trigger_error(htmlentities($e['message']), E_USER_ERROR);
}

?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   <example>
    <title>Rolling back to a <literal>SAVEPOINT</literal> example</title>
    <programlisting role="php">
<![CDATA[
<?php
$stid = oci_parse($conn, 'UPDATE mytab SET id = 1111');
oci_execute($stid, OCI_NO_AUTO_COMMIT);

// Create the savepoint
$stid = oci_parse($conn, 'SAVEPOINT mysavepoint');
oci_execute($stid, OCI_NO_AUTO_COMMIT);

$stid = oci_parse($conn, 'UPDATE mytab SET id = 2222');
oci_execute($stid, OCI_NO_AUTO_COMMIT);

// Use an explicit SQL statement to rollback to the savepoint
$stid = oci_parse($conn, 'ROLLBACK TO SAVEPOINT mysavepoint');
oci_execute($stid, OCI_NO_AUTO_COMMIT);

oci_commit($conn);  // mytab now has id of 1111
?>
]]>
    </programlisting>
   </example>
  </para>
 </refsect1>

 <refsect1 role="notes">
  &reftitle.notes;
  <note>
   <para>
    Transactions are automatically rolled back when you close the
    connection, or when the script ends, whichever is soonest. You
    need to explicitly call <function>oci_commit</function> to commit
    the transaction.
   </para>
   <para>
    Any call to <function>oci_execute</function> that uses
    <constant>OCI_COMMIT_ON_SUCCESS</constant> mode explicitly or by
    default will commit any previous uncommitted transaction.
   </para>
   <para>
    Any Oracle DDL statement such as <literal>CREATE</literal>
    or <literal>DROP</literal> will automatically commit any
    uncommitted transaction.
   </para>
  </note>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>oci_commit</function></member>
    <member><function>oci_execute</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
-->
