Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: pg_pconnect - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

pg_ping" width="11" height="7"/> <pg_parameter_status
Last updated: Sun, 25 Nov 2007

view this page in

pg_pconnect

(PHP 4, PHP 5)

pg_pconnect — 持続的な PostgreSQL 接続をオープンする

説明

resource pg_pconnect ( string $connection_string [, int $connect_type ] )

pg_pconnect() は PostgreSQL への接続を オープンします。この関数は、他の PostgreSQL関数 が必要とする 接続リソースを返します。

既存の接続と同じ connection_string を用いて pg_pconnect() が 2 回目にコールされた場合は、 既存の接続を返します。ただし、connect_typePGSQL_CONNECT_FORCE_NEW を指定している場合は 除きます。

持続的接続を有効にするには、php.ini のディレクティブ pgsql.allow_persistent を "On" に設定する必要があります(デフォルトは On です)。 持続的接続の最大数は php.ini ディレクティブ pgsql.max_persistent で設定可能です(デフォルトは制限無しを意味する -1 です)。 接続全体の数は php.ini ディレクティブ pgsql.max_links で設定可能です。

pg_close() は、 pg_pconnect() によりオープンされた持続的接続は 閉じません。

パラメータ

connection_string

すべてデフォルトのパラメータを使用する場合には connection_string を空にすることが可能です。 または 1 つ以上のパラメータを空白で区切って指定することも可能です。 個々のパラメータは keyword = value の形式で 設定します。等号の前後の空白はあってもなくてもかまいません。 空の値や空白を含む値を指定する場合は、その値をシングルクォートで 囲みます(例: keyword = 'a value')。値の中に シングルクォートやバックスラッシュが含まれる場合は、それらを バックスラッシュでエスケープする必要があります(例: \' および \\)。

現在利用できるパラメータは以下のとおりです。 host , hostaddr , port , dbname , user , password , connect_timeout , options , tty (無視されます), sslmode , requiressl (非推奨。代わりに sslmode を推奨します) および service 。これらのうち実際にどの パラメータが使えるかは、PostgreSQL のバージョンに依存します。

connect_type

PGSQL_CONNECT_FORCE_NEW が渡された場合は、 たとえ connection_string が既存の接続と まったく同一であっても新しい接続をオープンします。

返り値

成功した場合に PostgreSQL の接続リソース、失敗した場合に FALSE を返します。

Example#1 pg_pconnect() の使用法

<?php
$dbconn 
pg_pconnect("dbname=mary");
// "mary"という名前のデータベースに接続

$dbconn2 pg_pconnect("host=localhost port=5432 dbname=mary");
// "localhost"のポート"5432"にて"mary"という名前のデータベースに接続

$dbconn3 pg_pconnect("host=sheep port=5432 dbname=mary user=lamb password=foo");
// ユーザ名とパスワードを指定してホスト"sheep"上の"mary"という名前のデータベースに接続

$conn_string "host=sheep port=5432 dbname=test user=lamb password=bar";
$dbconn4 pg_pconnect($conn_string);
// ユーザ名とパスワードを指定してホスト"sheep"上の"test"という名前のデータベースへ接続
?>



pg_ping" width="11" height="7"/> <pg_parameter_status
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
pg_pconnect
ts at dev dot websafe dot pl
28-Oct-2007 07:22
<?php
//
// Using pg_pconnect in a class.
//
// Why this? Because the manual says:
//
//   If a second call is made to pg_pconnect() with the same
//   connection_string as an existing connection, the existing
//   connection will be returned unless you pass
//   PGSQL_CONNECT_FORCE_NEW as connect_type.
//
// This is not always true.
//
/**
 * MyClassA creates a postgresql connection using pg_pconnect
 * and stores the resulting resource id to $this->conn
 */
class MyClassA
{
    function
__construct($connection_string)
    {
       
$this->conn =
           
pg_pconnect($connection_string)
                or die(
'Wrong CONN_STRING');
    }
}

//
// Showing current php.ini settings to be sure
// that persistent connections s  are allowed.
// -1 means 'unlimited'
//
echo '<br>pgsql.allow_persistent: ' . ini_get('pgsql.allow_persistent');
echo
'<br>pgsql.max_persistent: ' . ini_get('pgsql.max_persistent');
echo
'<br>pgsql.max_links: ' . ini_get('pgsql.max_links');
echo
'<br><br>';

// setting one custom connection string for all objects
// (modify $connection_string to fit your needs)
$connection_string =
   
'host=localhost port=5432' .
   
' dbname=test user=test password=test';

// 
// Creating 10 MyClassA objects using the same $connection_string
//
$objArr = Array();
for (
$i = 0; $i < 10; $i++)
{
   
$objArr[] = new MyClassA($connection_string);
}

//
// Human readable result:
//
foreach($objArr as $id => $object)
{
   
printf(
       
'%s: Object %s: using db %s<br>',
       
get_class($object), $id, $object->conn
   
);
}

/* ------------------------------------------------------------- */
// The result
// pgsql.allow_persistent: 1
// pgsql.max_persistent: -1
// pgsql.max_links: -1
//
// MyClassA: Object 0: using db Resource id #2
// MyClassA: Object 1: using db Resource id #3
// MyClassA: Object 2: using db Resource id #4
// MyClassA: Object 3: using db Resource id #5
// MyClassA: Object 4: using db Resource id #6
// MyClassA: Object 5: using db Resource id #7
// MyClassA: Object 6: using db Resource id #8
// MyClassA: Object 7: using db Resource id #9
// MyClassA: Object 8: using db Resource id #10
// MyClassA: Object 9: using db Resource id #11
//
/* ------------------------------------------------------------- */
//
// Each MyClassA object will use its _own_ database Resource id
//
?>
Dennis Fogg
14-Oct-2007 05:27
As of Aug 2007, some suggestions from the postgresql forums
on pg_pconnect(), faster postgres connections, and connection pooling:

Summary:
http://archives.postgresql.org/pgsql-general/2007-08/msg01406.php

Good details: http://archives.postgresql.org/pgsql-general/2007-08/msg00660.php
Also: http://archives.postgresql.org/pgsql-general/2007-08/msg01489.php
info at karl-philipp dot de
06-Oct-2003 08:05
A contribution to the transaction issue raised by "garrett at bgb dot cc".

In a German book about PostgreSQL in connection with PHP (Cornelia Boenigk, PostgreSQL - Grundlagen, Praxis, Anwendungsentwicklung mit PHP) one can read in chapter 19.3 about persistent connections:
If the page processing aborts and the transaction is not finished yet, the next script using the same persistent connection will be considered as the continuation of the transaction. In particular a lock of a table will persist. The explanation is as follows: After the abort of the script no COMMIT or ROLLBACK was sent to the db server.

The author describes a hint to avoid the scenario above:
You can create a function for resolving transactions and locks erroneously not closed. For invoking the function after execution of a script it has to be registered with the function register_shutdown_function():

 http://de2.php.net/manual/en/function.register-shutdown-function.php
Spiros Ioannou
30-Jan-2003 10:40
Instead of reducing MaxClients in apache you may try to
reduce pgsql.max_links in php to at least the number of
postmasters. It should work and leave
you with more available httpds for static html pages.
raggaflo at libertysurf dot fr
30-Mar-2002 01:03
Be careful when using Apache/PHP dynamic module/PostgreSQL :
in httpd.conf (Apache conf) default MaxClients is 150, whereas default PG's max_connections is 32 which is much fewer than 150. You have to set max_connections to at least MaxClients (and pg's shared_buffers to 2*max_connections at least) to avoid PG's errors with pg_pconnect like : "Sorry, too many clients already connected"
etsysx dot i dot hate dot spam at teleline dot es
10-Mar-2002 06:56
To setup a high availability server with apache as a static module and postgreSQL, change httpd.conf and set MaxClients to less than max postgreSQL simultaneous connections (like 32 or 64).
This way pg_pconnect will allways return a valid handle under heavy traffic or under a request flow attack without wasting resources and without connection problems.
garrett at bgb dot cc
09-Feb-2002 09:39
If a transaction is in progress when page processing ends, is it aborted before the connection placed bak in the pool? Or is the connection added "as is"?

It would seem that the correct thing to do is to always 'ABORT' before adding to the pool.

As a note, this would be a good time to check and see if the connection is still open before readding it. Thus allowing closed connections to be cleaned up over time, instead of hanging around for ever as they do now.
zarathustra dot heuser at sita dot int
23-Nov-2001 11:15
I was having the same problem with persistent connections to an Oracle database. Compiling PHP with the option --enable-sigchild solved the problem.
herve at elma dot fr
04-Dec-2000 11:14
For resetting an apache web server and in same time the persistent connections you may use :
./apachectl graceful
dmitri at graphictype dot com
11-May-2000 08:12
How to reset persistent connections:

killall -USR1 httpd
tumor at iris dot kkt dot bme dot hu
30-Mar-2000 10:39
Well, pg_pconnect is ALMOST identical to pg_Connect. The difference is that when you make something nasty with the PSQL server (like reseting it) then pg_pConnect still tryes to use the previous connection. This creates some nice broken_pipe errors (and maybe some more). I do not know that resetting the http server would help it, I think it would. As pg_connect creates a new connection every time (that is why it is \"so slow\") this error does not occurs with it. Really this is not a bug, but perphaps you could do something with it, and it may help some people working with recently changing pg security settings... Good bug hunting...
clecerf at univ-paris8 dot fr
24-Mar-2000 03:00
The "Options" parameter. It seems to worry some users...
Refering to the libpq doc of Postgres, it is used to set trace/debug options to be sent to backend.
Probably no use for a PHP3 programmer.

pg_ping" width="11" height="7"/> <pg_parameter_status
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites