|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-06-11 22:45 UTC] jonarobinson at rim dot com
Description:
------------
Once your handle gets garbage collected by PHP (falls off the method scope), subsequent calls to oci_connect will generate new connections.
This is true in PHP 5.2.13. I wasn't able to verify on PHP 5.3.
Test script:
---------------
connect();
connect();
$dbh1 = connect();
$dbh2 = connect();
function connect()
{
$conn_info = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = localhost) (PORT = 1521)) (CONNECT_DATA = (SERVICE_NAME = TEST)))';
$dbh = oci_connect('username', 'password', $conn_info);
echo "Our handle is: $dbh<br/>\n";
return $dbh;
}
function getConnection($sid)
Expected result:
----------------
Our handle is: Resource id #35
Our handle is: Resource id #35
Our handle is: Resource id #35
Our handle is: Resource id #35
Actual result:
--------------
Our handle is: Resource id #35
Our handle is: Resource id #36
Our handle is: Resource id #37
Our handle is: Resource id #37
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 15:00:02 2025 UTC |
I found a work around. By hanging on to a reference to the DB handle (whether you use it or not) it will prevent the DB handle from being garbage collected in your app. Thus subsequent calls to oci_connect will use the cached resource as expected. If you're only using one connection you can do something like this: $dbh_keep_alive = connect(); // Hang on to a reference - if it gets garbage collected then our connection pooling will stop working If you're using multiple connections like me than you'll need to keep a cache of connections used. This should do the trick: connect($username, $password, $conn_info) { $dbh = oci_connect($username, $password, $conn_info); $key = hash('md5', "$username|$password|$conn_info"); $GLOBALS[$key] = $dbh; return $dbh; }