php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52056 garbage collection interferes with oci_connect connection pooling
Submitted: 2010-06-11 22:45 UTC Modified: 2010-06-15 19:28 UTC
From: jonarobinson at rim dot com Assigned: sixd (profile)
Status: Not a bug Package: OCI8 related
PHP Version: 5.2.13 OS: RHEL 4 R5
Private report: No CVE-ID: None
 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-11 23:10 UTC] jonarobinson at rim dot com
-Summary: gargbage collection interferes with oci_connect +Summary: garbage collection interferes with oci_connect connection pooling
 [2010-06-11 23:10 UTC] jonarobinson at rim dot com
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;
}
 [2010-06-15 19:28 UTC] sixd@php.net
-Status: Open +Status: Bogus -Assigned To: +Assigned To: sixd
 [2010-06-15 19:28 UTC] sixd@php.net
This is expected behavior.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 22:01:28 2024 UTC