php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #818 pg_Connect() doesn't return unique connection
Submitted: 1998-10-05 10:36 UTC Modified: 2001-02-10 12:31 UTC
From: bschaffner at accentonline dot com Assigned: zeev (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 3.0.4 OS: FreeBSD 2.2.6
Private report: No CVE-ID: None
 [1998-10-05 10:36 UTC] bschaffner at accentonline dot com
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-


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-02-10 12:31 UTC] jimw@php.net
this was a duplicate of 729.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC