[PHP-DEV] Bug #818: pg_Connect() doesn't return unique connection From: bschaffner <email protected>
Date: 10/05/98

From: bschaffner <email protected>
Operating system: FreeBSD 2.2.6
PHP version: 3.0.4
PHP Bug Type: PostgreSQL related
Bug description: pg_Connect() doesn't return unique connection

pg_Connect() doesn't always return a unique connection identifier. This is a problem for code that uses nested
connections, or code that "hides" connections by using
objects.

One work-around is to not explicitly call pg_Close() from the nested connection. The other is to do away with this feature - because it smells a lot like pg_pConnect().

Here's a small piece of code:

<?

$HOST = "localhost";
$PORT = "5432";
$DATABASE = "template1";

function Nested() {
        global $HOST, $PORT, $DATABASE;
        
        $conn = pg_Connect($HOST, $PORT, "", "", $DATABASE);
        if ($conn) {
                echo "Nested id: $conn\n";
                pg_Close($conn);
        }
}

function SomeFunc() {
        global $HOST, $PORT, $DATABASE;
        $conn = pg_Connect($HOST, $PORT, "", "", $DATABASE);
        if ($conn) {
                echo "First id: $conn\n";
                Nested();
                pg_Close($conn);
        }
}

SomeFunc();

?>

Running this code yields:

First id: 1
Nested id: 1
<br>
<b>Warning</b>: 1 is not a PostgreSQL link index in <b>foo.php3</b> on line <b>23</b><br>

The expected output is:

First id: 1
Nested id: 2

The cause is the lookup performed inside pg_Connect(). Whenever a connection is established, it's stored in a hash table. Then, when a new connection is requested, the connection parameters are looked up in the hash table, and if a match is found, an existing connection id is returned. You can see this behaviour by changing one of the above pg_Connect() lines to:

$conn = pg_Connect("127.0.0.1", $PORT, "", "", $DATABASE);

This will cause the lookup to fail because localhost looks different from 127.0.0.1 in the lookup.

I have a feeling this may be something leftover from before pg_pConnect()...

-Brian Schaffner-

--
PHP Development Mailing List   http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe <email protected>
For help: php-dev-help <email protected>