Codex73
Codex73

Reputation: 5766

Persistent database connection for Ajax Calls

I have ajax call which pulls from the processing script 'getajax.php'.

Call is made to 'getajax.php' script which has the db connection details, select, functions, etc.

My question is:

Everytime a call is received by 'getajax.php' it will go through mysql_connect, mysql_select, then queries.

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

How can I avoid mysql connection to be opened everytime a call is made, reusing one existing connection for all calls.

Trying to have one call to:

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

How can I open a persistent connection on parent so 'getajax.php' script just reususes this connection without running these mysql commands over and over.

Unsure how to aproach.

Thanks All!

Upvotes: 5

Views: 2272

Answers (2)

halfdan
halfdan

Reputation: 34214

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

Upvotes: 2

Brian Agnew
Brian Agnew

Reputation: 272297

It sounds like you need connection pooling, where a set of connections is always maintained for clients. It reduces the overhead of opening a new connection. You would normally not have a connection per client, but a set of connections configured for a number of simultaneously requesting clients.

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

Upvotes: 1

Related Questions