Edit report at http://bugs.php.net/bug.php?id=52056&edit=1
ID: 52056
User updated by: jonarobinson at rim dot com
Reported by: jonarobinson at rim dot com
-Summary: gargbage collection interferes with oci_connect
+Summary: garbage collection interferes with oci_connect
connection pooling
Status: Open
Type: Bug
Package: OCI8 related
Operating System: RHEL 4 R5
PHP Version: 5.2.13
New Comment:
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;
}
Previous Comments:
------------------------------------------------------------------------
[2010-06-11 22:45:09] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52056&edit=1