note 98431 modified in function.oci-connect by danbrown

From: Date: Tue, 15 Jun 2010 18:32:10 +0000
Subject: note 98431 modified in function.oci-connect by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170149@lists.php.net to get a copy of this message
Regarding the following statement in the documentation: "The second and subsequent calls to oci_connect() with the same parameters will return the connection handle returned from the first call." There's one caveat here. Subsequent calls to oci_connect() will only return the same connection handle as the first call as long as a reference is held to the original handle. For example, the following code will generate *one* connection handle: <?php $dbh = oci_connect($username, $password, $conn_info); // Do stuff $dbh = oci_connect$username, $password, $conn_info); // Do more stuff The follow code will generate *two* connection handles: getData(); // Do stuff getData(); // Do more stuff getData() { $dbh = oci_connect($username, $password, $conn_info); // Do stuff } ?> This is the result of PHP garbage collecting the handle at the end of the method scope. If you want to isolate your DB layer through function calls but still want to leverage the fact that oci_connect can return the same handle, just keep a reference to the handle like so: <?php getData($username, $password, $conn_info) { $dbh = oci_connect($username, $password, $conn_info); $key = hash('md5', "$username|$password|$conn_info"); $GLOBALS[$key] = $dbh; // Do stuff } ?> I originally logged this as a bug but apparently this is the expected behaviour, likely because oci_close($dbh) just calls unset($dbh). --was-- "The second and subsequent calls to oci_connect() with the same parameters will return the connection handle returned from the first call." There's one caveat here. Subsequent calls to oci_connect() will only return the same connection handle as the first call as long as a reference is held to the original handle. For example, the following code will generate *one* connection handle: $dbh = oci_connect($username, $password, $conn_info); // Do stuff $dbh = oci_connect$username, $password, $conn_info); // Do more stuff The follow code will generate *two* connection handles: getData(); // Do stuff getData(); // Do more stuff getData() { $dbh = oci_connect($username, $password, $conn_info); // Do stuff } This is the result of PHP garbage collecting the handle at the end of the method scope. If you want to isolate your DB layer through function calls but still want to leverage the fact that oci_connect can return the same handle, just keep a reference to the handle like so: getData($username, $password, $conn_info) { $dbh = oci_connect($username, $password, $conn_info); $key = hash('md5', "$username|$password|$conn_info"); $GLOBALS[$key] = $dbh; // Do stuff } I originally logged this as a bug but apparently this is the expected behaviour, likely because oci_close($dbh) just calls unset($dbh). http://php.net/manual/en/function.oci-connect.php

« previous php.notes (#170149) next »