php-doc-en/reference/oci8/functions/oci-close.xml

257 lines
6.9 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<refentry xml:id="function.oci-close" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>oci_close</refname>
<refpurpose>Closes an Oracle connection</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>bool</type><methodname>oci_close</methodname>
<methodparam><type>resource</type><parameter>connection</parameter></methodparam>
</methodsynopsis>
<para>
Unsets <parameter>connection</parameter>. The underlying database
connection is closed if no other resources are using it and if it
was created with <function>oci_connect</function>
or <function>oci_new_connect</function>.
</para>
<para>
It is recommended to close connections that are no longer needed
because this makes database resources available for other users.
</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;
<example>
<title>Closing a connection</title>
<para>
Resources associated with a connection should be closed to ensure
the underlying database connection is properly terminated and the
database resources are released.
</para>
<programlisting role="php">
<![CDATA[
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, 'SELECT * FROM departments');
$r = oci_execute($stid);
oci_fetch_all($stid, $res);
var_dump($res);
// Free the statement identifier when closing the connection
oci_free_statement($stid);
oci_close($conn);
?>
]]>
</programlisting>
</example>
<example>
<title>Database connections are not closed until all references are closed</title>
<para>
The internal refcount of a connection identifier must be zero
before the underlying connection to the database is closed.
</para>
<programlisting role="php">
<![CDATA[
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, 'SELECT * FROM departments'); // this increases the refcount on $conn
oci_execute($stid);
oci_fetch_all($stid, $res);
var_dump($res);
oci_close($conn);
// $conn is no long usable in the script but the underlying database
// connection is still held open until $stid is freed.
var_dump($conn); // prints NULL
// While PHP sleeps, querying the Oracle V$SESSION view in a
// terminal window will show that the database user is still connected.
sleep(10);
// When $stid is freed, the database connection is physically closed
oci_free_statement($stid);
// While PHP sleeps, querying the Oracle V$SESSION view in a
// terminal window will show that the database user has disconnected.
sleep(10);
?>
]]>
</programlisting>
</example>
<example>
<title>Closing a connection opened more than once</title>
<para>
When database credentials are reused, both connections must be closed
before the underlying database connection is closed.
</para>
<programlisting role="php">
<![CDATA[
<?php
$conn1 = oci_connect('hr', 'welcome', 'localhost/XE');
// Using the same credentials reuses the same underlying database connection
// Any uncommitted changes done on $conn1 will be visible in $conn2
$conn2 = oci_connect('hr', 'welcome', 'localhost/XE');
// While PHP sleeps, querying the Oracle V$SESSION view in a
// terminal window will show that only one database user is connected.
sleep(10);
oci_close($conn1); // doesn't close the underlying database connection
var_dump($conn1); // prints NULL because the variable $conn1 is no longer usable
var_dump($conn2); // displays that $conn2 is still a valid connection resource
?>
]]>
</programlisting>
</example>
<example>
<title>Connections are closed when variables go out of scope</title>
<para>
When all variables referencing a connection go out of scope and
are freed by PHP, a rollback occurs (if necessary) and the
underlying connection to the database is closed.
</para>
<programlisting role="php">
<![CDATA[
<?php
function myfunc() {
$conn = oci_connect('hr', 'hrpwd', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, 'UPDATE mytab SET id = 100');
oci_execute($stid, OCI_NO_AUTO_COMMIT);
return "Finished";
}
$r = myfunc();
// At this point a rollback occurred and the underlying database connection was released.
print $r; // displays the function return value "Finished"
?>
]]>
</programlisting>
</example>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
Variables that have a dependency on the connection identifier,
such as statement identifiers returned
by <function>oci_parse</function>, must also be freed before the
underlying database connection is closed.
</para>
</note>
<note>
<para>
Prior to version PHP 5.1.2 (PECL OCI8
1.1) <function>oci_close</function> was a no-op. In more recent
versions it correctly closes the Oracle
connection. Use <link linkend="ini.oci8.old-oci-close-semantics">oci8.old_oci_close_semantics</link>
option to restore old behavior of this function.
</para>
</note>
<note>
<para>
The <function>oci_close</function> function does not close the
underlying database connections created
with <function>oci_pconnect</function>.
</para>
</note>
<note>
<para>
In PHP versions before 5.0.0 you must
use <function>ocilogoff</function> instead. &oci.name.compat.note;
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>oci_connect</function></member>
<member><function>oci_free_statement</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
-->