Permanent database connection for Ajax calls

I have an ajax call that fetches script 'getajax.php' from processing.

The call is called by the "getajax.php" script, which contains connection information for db, select, functions, etc.

My question is:

Each time a call is received by "getajax.php", it will go through mysql_connect, mysql_select, then the requests.

Is this the correct aproach to handle thousands of simultaneous calls?

How can I avoid opening a mysql connection on every call, reusing one existing connection for all calls.

Trying to have one call:

$dbconnect = mysql_connect('host','user','pass'); mysql_select_db('databasename') or die( "Unable to select database"); 

How can I open a persistent connection with the parent, so the 'getajax.php' script just reuses this connection without restarting these mysql commands again and again.

Not sure how to do this.

Thanks everyone!

+4
source share
2 answers

You can use mysql_pconnect ( http://www.php.net/manual/en/function.mysql-pconnect.php ), which creates a permanent database connection.

+2
source

It looks like you need a connection pool where a set of connections is always supported for clients. This reduces the overhead of opening a new connection. Usually you did not have a client connection, but the set of connections is configured for several requesting clients at the same time.

See here for more details on mysql_pconnect and here for a related SO question.

+1
source

All Articles