views:

3174

answers:

4

Is it possible to cache database connections when using PHP like you would in a J2EE container? If so, how?

+2  A: 

You cannot instantiate connection pools manually.

But you can use the "built in" connection pooling with the mysql_pconnect function.

Marcel
A: 

Have you looked at persistent connects using mysql_pconnect()?

Chris Bartow
+2  A: 

I suppose you're using mod_php, right?

When a PHP file finishes executing all it's state is killed so there's no way (in PHP code) to do connection pooling. Instead you have to rely on extensions.

You can mysql_pconnect so that your connections won't get closed after the page finishes, that way they get reused in the next request.

This might be all that you need but this isn't the same as connection pooling as there's no way to specify the number of connections to maintain opened.

Cheers.

Julio César
Can the connection be reused by different users?If so,why the manual says that we should configure MySQL to avoid too many connections?
Shore
+1  A: 

There is no connection pooling in php.
mysql_pconnect and connection pooling are two different things. There are many problems connected with mysql_pconnect and first you should read the manual and carefully use it, but this is not connection pooling.

Connection pooping is a technique that the application server manages the connections. Then the app requires a connection from the application server and the server returns a reference.

So no connection pooling in php.

As Julio said apache releases all resources when the request ends for the current reques. You can use mysql_pconnect but you are limited with that function and you must be very careful. Other choice is to use singleton pattern, but none of this is pooling.

This is a good article: http://blogs.oracle.com/opal/2007/01/highly_scalable_connection_poo.html

Also read this one http://www.apache2.es/2.2.2/mod/mod_dbd.html

darko petreski
hehe, you said pooping ;)
grapefrukt