mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 05:26:57 +08:00
190 lines
20 KiB
HTML
190 lines
20 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
<html>
|
||
<head>
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
<title>关闭 Oracle 连接</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.oci-close" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">oci_close</h1>
|
||
<p class="verinfo">(PHP 5, PHP 7, PECL OCI8 >= 1.1.0)</p><p class="refpurpose"><span class="refname">oci_close</span> — <span class="dc-title">关闭 Oracle 连接</span></p>
|
||
|
||
</div>
|
||
<div class="refsect1 description" id="refsect1-function.oci-close-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<span class="methodname"><strong>oci_close</strong></span>
|
||
( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$connection</code></span>
|
||
) : <span class="type">bool</span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
<span class="function"><strong>oci_close()</strong></span> 将 Oracle 连接
|
||
<code class="parameter">connection</code> 关闭。
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
自版本 1.1 起 <span class="function"><strong>oci_close()</strong></span> 正确关闭
|
||
Oracle 连接。使用 <a href="oci8.configuration.html#ini.oci8.old-oci-close-semantics" class="link">oci8.old_oci_close_semantics</a>
|
||
选项来恢复本函数的旧行为。
|
||
</p>
|
||
</p></blockquote>
|
||
<p class="para">
|
||
成功时返回 <strong><code>TRUE</code></strong>, 或者在失败时返回 <strong><code>FALSE</code></strong>。
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
在 PHP 5.0.0 之前的版本必须使用 <span class="function"><a href="ocilogoff.html" class="function">ocilogoff()</a></span>
|
||
替代本函数。该函数名仍然可用,为向下兼容作为
|
||
<span class="function"><strong>oci_close()</strong></span> 的别名。不过其已被废弃,不推荐使用。
|
||
</p>
|
||
</p></blockquote>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 parameters" id="refsect1-function.oci-close-parameters">
|
||
<h3 class="title">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">connection</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
An Oracle connection identifier returned by
|
||
<span class="function"><a href="oci_connect.html" class="function">oci_connect()</a></span>, <span class="function"><a href="oci_pconnect.html" class="function">oci_pconnect()</a></span>,
|
||
or <span class="function"><a href="oci_new_connect.html" class="function">oci_new_connect()</a></span>.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.oci-close-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
成功时返回 <strong><code>TRUE</code></strong>, 或者在失败时返回 <strong><code>FALSE</code></strong>。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 examples" id="refsect1-function.oci-close-examples">
|
||
<h3 class="title">范例</h3>
|
||
<div class="example" id="example-2394">
|
||
<p><strong>Example #1 Closing a connection</strong></p>
|
||
<div class="example-contents"><p>
|
||
Resources associated with a connection should be closed to ensure
|
||
the underlying database connection is properly terminated and the
|
||
database resources are released.
|
||
</p></div>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br /><br />$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$e </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_error</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">[</span><span style="color: #DD0000">'message'</span><span style="color: #007700">], </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">), </span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT * FROM departments'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$r </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_fetch_all</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Free the statement identifier when closing the connection<br /></span><span style="color: #0000BB">oci_free_statement</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
<div class="example" id="example-2395">
|
||
<p><strong>Example #2 Database connections are not closed until all references are closed</strong></p>
|
||
<div class="example-contents"><p>
|
||
The internal refcount of a connection identifier must be zero
|
||
before the underlying connection to the database is closed.
|
||
</p></div>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br /><br />$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$e </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_error</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">[</span><span style="color: #DD0000">'message'</span><span style="color: #007700">], </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">), </span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT * FROM departments'</span><span style="color: #007700">); </span><span style="color: #FF8000">// this increases the refcount on $conn<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_fetch_all</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">oci_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// $conn is no long usable in the script but the underlying database<br />// connection is still held open until $stid is freed.<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">); </span><span style="color: #FF8000">// prints NULL <br /><br />// While PHP sleeps, querying the Oracle V$SESSION view in a<br />// terminal window will show that the database user is still connected.<br /></span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// When $stid is freed, the database connection is physically closed<br /></span><span style="color: #0000BB">oci_free_statement</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">); <br /><br /></span><span style="color: #FF8000">// While PHP sleeps, querying the Oracle V$SESSION view in a<br />// terminal window will show that the database user has disconnected.<br /></span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
<div class="example" id="example-2396">
|
||
<p><strong>Example #3 Closing a connection opened more than once</strong></p>
|
||
<div class="example-contents"><p>
|
||
When database credentials are reused, both connections must be closed
|
||
before the underlying database connection is closed.
|
||
</p></div>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br /><br />$conn1 </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Using the same credentials reuses the same underlying database connection<br />// Any uncommitted changes done on $conn1 will be visible in $conn2<br /></span><span style="color: #0000BB">$conn2 </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// While PHP sleeps, querying the Oracle V$SESSION view in a<br />// terminal window will show that only one database user is connected.<br /></span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">oci_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn1</span><span style="color: #007700">); </span><span style="color: #FF8000">// doesn't close the underlying database connection<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn1</span><span style="color: #007700">); </span><span style="color: #FF8000">// prints NULL because the variable $conn1 is no longer usable<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn2</span><span style="color: #007700">); </span><span style="color: #FF8000">// displays that $conn2 is still a valid connection resource<br /><br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
<div class="example" id="example-2397">
|
||
<p><strong>Example #4 Connections are closed when variables go out of scope</strong></p>
|
||
<div class="example-contents"><p>
|
||
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.
|
||
</p></div>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">myfunc</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'hrpwd'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /> if (!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$e </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_error</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">[</span><span style="color: #DD0000">'message'</span><span style="color: #007700">], </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">), </span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br /> }<br /><br /> </span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'UPDATE mytab SET id = 100'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">OCI_NO_AUTO_COMMIT</span><span style="color: #007700">);<br /> return </span><span style="color: #DD0000">"Finished"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$r </span><span style="color: #007700">= </span><span style="color: #0000BB">myfunc</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">// At this point a rollback occurred and the underlying database connection was released.<br /><br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$r</span><span style="color: #007700">; </span><span style="color: #FF8000">// displays the function return value "Finished"<br /><br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 notes" id="refsect1-function.oci-close-notes">
|
||
<h3 class="title">注释</h3>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
Variables that have a dependency on the connection identifier,
|
||
such as statement identifiers returned
|
||
by <span class="function"><a href="oci_parse.html" class="function">oci_parse()</a></span>, must also be freed before the
|
||
underlying database connection is closed.
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
Prior to version PHP 5.1.2 (PECL OCI8
|
||
1.1) <span class="function"><strong>oci_close()</strong></span> was a no-op. In more recent
|
||
versions it correctly closes the Oracle
|
||
connection. Use <a href="oci8.configuration.html#ini.oci8.old-oci-close-semantics" class="link">oci8.old_oci_close_semantics</a>
|
||
option to restore old behavior of this function.
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
The <span class="function"><strong>oci_close()</strong></span> function does not close the
|
||
underlying database connections created
|
||
with <span class="function"><a href="oci_pconnect.html" class="function">oci_pconnect()</a></span>.
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
In PHP versions before 5.0.0 you must
|
||
use <span class="function"><a href="ocilogoff.html" class="function">ocilogoff()</a></span> instead. 在当前版本中,旧的函数名还可以被使用,但已经被废弃并不建议使用。
|
||
</p>
|
||
</p></blockquote>
|
||
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 seealso" id="refsect1-function.oci-close-seealso">
|
||
<h3 class="title">参见</h3>
|
||
<p class="para">
|
||
<ul class="simplelist">
|
||
<li class="member"><span class="function"><a href="oci_connect.html" class="function" rel="rdfs-seeAlso">oci_connect()</a> - 建立一个到 Oracle 服务器的连接</span></li>
|
||
<li class="member"><span class="function"><a href="oci_free_statement.html" class="function" rel="rdfs-seeAlso">oci_free_statement()</a> - 释放关联于语句或游标的所有资源</span></li>
|
||
</ul>
|
||
</p>
|
||
</div>
|
||
|
||
</div></div></div></body></html> |