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

[{'answer_id': 39837, 'author': 'Marcel', 'author_id': 2554, 'author_profile': 'https://Stackoverflow.com/users/2554', 'pm_score': 0, 'selected': False, 'text': '<p>You cannot instantiate connection pools manually.</p>\n\n<p>But you can use the "built in" connection pooling with the <a href="http://de.php.net/manual/en/function.mysql-pconnect.php" rel="nofollow noreferrer">mysql_pconnect</a> function.</p>\n'}, {'answer_id': 39851, 'author': 'Julio César', 'author_id': 2148, 'author_profile': 'https://Stackoverflow.com/users/2148', 'pm_score': 4, 'selected': False, 'text': '<p>I suppose you\'re using mod_php, right?</p>\n\n<p>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.</p>\n\n<p>You can <a href="http://www.php.net/manual/en/function.mysql-pconnect.php" rel="noreferrer">mysql_pconnect</a> so that your connections won\'t get closed after the page finishes, that way they get reused in the next request.</p>\n\n<p>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.</p>\n'}, {'answer_id': 3819681, 'author': 'darpet', 'author_id': 312741, 'author_profile': 'https://Stackoverflow.com/users/312741', 'pm_score': 6, 'selected': False, 'text': '<p>There is no <strong>connection pooling</strong> in php.<br>\n<strong>mysql_pconnect</strong> and <strong>connection pooling</strong> are two different things.\nThere are many problems connected with <code>mysql_pconnect</code> and first you should read the manual and carefully use it, but this is not connection pooling.</p>\n\n<p>Connection pooling is a technique where the application server manages the connections. When the application needs a connection it asks the application server for it and the application server returns one of the pooled connections if there is one free.</p>\n\n<p>We can do connection scaling in php for that please go through following link: <a href="http://www.oracle.com/technetwork/articles/dsl/white-php-part1-355135.html" rel="noreferrer">http://www.oracle.com/technetwork/articles/dsl/white-php-part1-355135.html</a></p>\n\n<p>So no connection pooling in php.</p>\n\n<p>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.</p>\n\n<p>This is a good article: <a href="https://blogs.oracle.com/opal/highly-scalable-connection-pooling-in-php" rel="noreferrer">https://blogs.oracle.com/opal/highly-scalable-connection-pooling-in-php</a></p>\n\n<p>Also read this one <a href="http://www.apache2.es/2.2.2/mod/mod_dbd.html" rel="noreferrer">http://www.apache2.es/2.2.2/mod/mod_dbd.html</a></p>\n'}, {'answer_id': 10339592, 'author': 'minorgod', 'author_id': 751543, 'author_profile': 'https://Stackoverflow.com/users/751543', 'pm_score': 4, 'selected': False, 'text': '<p>Persistent connections are nothing like connection pooling. A persistent connection in php will only be reused if you make multiple db connects within the same request/script execution context. In most typical web dev scenarios you\'ll max out your connections way faster if you use mysql_pconnect because your script will have no way to get a reference to any open connections on your next request. The best way to use db connections in php is to make a singleton instance of a db object so that the connection is reused within the context of your script execution. This still incurs at least 1 db connect per request, but it\'s better than making multiple db connects per reqeust. </p>\n\n<p>There is no real db connection pooling in php due to the nature of php. Php is not an application server that can sit there in between requests and manage references to a pool of open connections, at least not without some kind of major hack. I think  in theory you could write an app server in php and run it as a commandline script that would just sit there in the background and keep a bunch of db connections open and pass references to them to your other scripts, but I don\'t know if that would be possible in practice, how you\'d pass the references from your commandline script to other scripts, and I sort of doubt it would perform well even if you could pull it off. Anyway that\'s mostly speculation.  I did just notice the link someone else posted to an apache module to allow connection pooling for prefork servers such as php. Looks interesting:\n<a href="https://github.com/junamai2000/mod_namy_pool#readme" rel="noreferrer">https://github.com/junamai2000/mod_namy_pool#readme</a></p>\n'}, {'answer_id': 20977005, 'author': 'user193130', 'author_id': 2891365, 'author_profile': 'https://Stackoverflow.com/users/2891365', 'pm_score': 3, 'selected': False, 'text': '<p>You can use <a href="http://www.php.net/manual/en/book.mysqli.php" rel="noreferrer">MySQLi</a>.</p>\n\n<p>For more info, scroll down to <em>Connection pooling</em> section @ <a href="http://www.php.net/manual/en/mysqli.quickstart.connections.php#example-1622" rel="noreferrer">http://www.php.net/manual/en/mysqli.quickstart.connections.php#example-1622</a></p>\n\n<p>Note that Connection pooling is also dependent on your server (i.e. Apache httpd) and its configuration.</p>\n'}, {'answer_id': 55300921, 'author': 'Amir Fo', 'author_id': 7580839, 'author_profile': 'https://Stackoverflow.com/users/7580839', 'pm_score': 0, 'selected': False, 'text': "<p>I would like to suggest <code>PDO::ATTR_PERSISTENT</code></p>\n\n<p>Persistent connections are links that do not close when the execution of your script ends. When a persistent connection is requested, PHP checks if there's already an identical persistent connection (that remained open from earlier) - and if it exists, it uses it. If it does not exist, it creates the link.</p>\n"}, {'answer_id': 56729603, 'author': 'Engr Syed Rowshan Ali', 'author_id': 10270165, 'author_profile': 'https://Stackoverflow.com/users/10270165', 'pm_score': 0, 'selected': False, 'text': '<p>Connection pooling works at MySQL server side like this.</p>\n\n<ol>\n<li>If persistence connection is enabled into MySQL server config then MySQL keep a connection open and in sleep state after requested client (php script) finises its work and die.</li>\n<li>When a 2nd request comes with same credential data (Same User Name, Same Password, Same Connection Parameter, Same Database name, Maybe from same IP, I am not sure about the IP) Then MySQL pool the previous connection from sleep state to active state and let the client use the connection. This helps MySQL to save time for initial resource for connection and reduce the total number of connection.</li>\n</ol>\n\n<p>So the connection pooling option is actually available at MySQL server side. At PHP code end there is no option. mysql_pconnect() is just a wrapper that inform PHP to not send connection close request signal at the end of script run. </p>\n'}, {'answer_id': 60347739, 'author': 'Raju', 'author_id': 8734732, 'author_profile': 'https://Stackoverflow.com/users/8734732', 'pm_score': 1, 'selected': False, 'text': '<p>If an unused persistent connection for a given combination of "host, username, password, socket, port and default database can not be found" in the open connection pool, then only mysqli opens a new connection otherwise it would reuse already open available persistent connections, which is in a way similar to the concept of connection pooling. The use of persistent connections can be enabled and disabled using the PHP directive mysqli.allow_persistent. The total number of connections opened by a script can be limited with mysqli.max_links (this may be interesting to you to address max_user_connections issue hitting hosting server\'s limit). The maximum number of persistent connections per PHP process can be restricted with mysqli.max_persistent. </p>\n\n<p>In wider programming context, it\'s a task of web/app server however in this context, it\'s being handled by mysqli directive of PHP itself in a way supporting connection re-usability. You may also implement a singleton class to get a static instance of connection to reuse just like in Java. Just want to remind that java also doesn\'t support connection pooling as part of its standard JDBC, they\'re being different module/layers on top of JDBC drivers.</p>\n\n<p>Coming to PHP, the good thing is that for the common databases in the PHP echosystem it does support Persistent Database Connections which persists the connection for 500 requests (config of max_requests in php.ini) and this avoids creating a new connection in each request. So check it out in docs in detail, it solves most of your challenges. Please note that PHP is not so much sophisticated in terms of extensive multi-threading mechanism and concurrent processing together with powerful asynchronous event handling, when compared to strictly object oriented Java. So in a way it is very less effective for PHP to have such in-built mechanism like pooling.</p>\n'}, {'answer_id': 72416232, 'author': 'Dannyboy', 'author_id': 2769590, 'author_profile': 'https://Stackoverflow.com/users/2769590', 'pm_score': 0, 'selected': False, 'text': '<p>For features such as connection pooling - you need to install swoole extension first: <a href="https://openswoole.com/" rel="nofollow noreferrer">https://openswoole.com/</a></p>\n<p>It adds async features to php.</p>\n<p>After that its trivial to add mysql and redis connection pooling:<br />\n<a href="https://github.com/open-smf/connection-pool" rel="nofollow noreferrer">https://github.com/open-smf/connection-pool</a></p>\n<p>Some PHP frameworks come with pooling built-in: <a href="https://hyperf.wiki/2.2/#/en/pool" rel="nofollow noreferrer">https://hyperf.wiki/2.2/#/en/pool</a></p>\n'}]
['https://Stackoverflow.com/questions/39753', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4263/']