<p>I'm using PHP's PDO layer for data access in a project, and I've been reading up on it and seeing that it has good innate support for persistent DB connections. I'm wondering when/if I should use them. Would I see performance benefits in a CRUD-heavy app? Are there downsides to consider, perhaps related to security?</p>

<p>If it matters to you, I'm using MySQL 5.x.</p>

[{'answer_id': 50305, 'author': 'Josh Hinman', 'author_id': 2527, 'author_profile': 'https://Stackoverflow.com/users/2527', 'pm_score': 2, 'selected': False, 'text': '<p>Creating connections to the database is a fairly expensive operation. Persistent connections are a good idea. In the ASP.Net and Java world, we have "connection pooling", which is roughly the same thing, and also a good idea.</p>\n'}, {'answer_id': 50328, 'author': 'Mike H', 'author_id': 4563, 'author_profile': 'https://Stackoverflow.com/users/4563', 'pm_score': 2, 'selected': False, 'text': '<p>IMO, The real answer to this question is whatever works best for you app. I would recommend you benchmark your app using both persistent and non-persistent connections. </p>\n\n<p>Maggie Nelson @ <a href="http://www.objectivelyoriented.com/2008/08/to_persist_or_not_to_persist.html" rel="nofollow noreferrer">Objectively Oriented</a> posted about this in August and <a href="http://www.robertswarthout.com/2008/08/benchmarking-of-mysql-persistent-connections-vs-non-persistent-connections/" rel="nofollow noreferrer">Robert Swarthout</a> made an accompanying post with some hard numbers. Both are pretty good reads.</p>\n'}, {'answer_id': 51583, 'author': 'BlaM', 'author_id': 999, 'author_profile': 'https://Stackoverflow.com/users/999', 'pm_score': 7, 'selected': True, 'text': '<p>You could use this as a rough "ruleset":</p>\n\n<p><strong>YES</strong>, use persistent connections, if:</p>\n\n<ul>\n<li>There are only few applications/users accessing the database, i.e. you will not result in 200 open (but probably idle) connections, because there are 200 different users shared on the same host.</li>\n<li>The database is running on another server that you are accessing over the network</li>\n<li>An (one) application accesses the database very often</li>\n</ul>\n\n<p><strong>NO</strong>, don\'t use persistent connections, if:</p>\n\n<ul>\n<li>Your application only needs to access the database 100 times an hour. </li>\n<li>You have many webservers accessing one database server</li>\n<li>You\'re using Apache in prefork mode. It uses one connection for each child process, which can ramp up fairly quickly. (via @Powerlord in the comments)</li>\n</ul>\n\n<p>Using persistent connections is considerable faster, especially if you are accessing the database over a network. It doesn\'t make so much difference if the database is running on the same machine, but it is still a little bit faster. However - as the name says - the connection is persistent, i.e. it stays open, even if it is not used.</p>\n\n<p>The problem with that is, that in "default configuration", MySQL only allows 1000 parallel "open channels". After that, new connections are refused (You can tweak this setting). So if you have - say - 20 Webservers with each 100 Clients on them, and every one of them has just one page access per hour, simple math will show you that you\'ll need 2000 parallel connections to the database. That won\'t work. </p>\n\n<p>Ergo: Only use it for applications with lots of requests.</p>\n'}, {'answer_id': 51897, 'author': 'Markus', 'author_id': 2490, 'author_profile': 'https://Stackoverflow.com/users/2490', 'pm_score': 1, 'selected': False, 'text': '<p>In my humble opinion:</p>\n\n<p>When using PHP for web development, most of your connection will only "live" for the life of the page executing. A persistant connection is going to cost you a lot of overhead as you\'ll have to put it in the session or some such thing.</p>\n\n<p>99% of the time a single non-persistant connection that dies at the end of the page execution will work just fine.</p>\n\n<p>The other 1% of the time, you probably should not be using PHP for the app, and there is no perfect solution for you.</p>\n'}, {'answer_id': 278828, 'author': 'ejunker', 'author_id': 796, 'author_profile': 'https://Stackoverflow.com/users/796', 'pm_score': 0, 'selected': False, 'text': '<p>I was going to ask this same question but rather than ask the same question again I\'ll just add some information that I\'ve found.</p>\n\n<ul>\n<li><a href="http://www.mysqlperformanceblog.com/2006/11/12/are-php-persistent-connections-evil/" rel="nofollow noreferrer">Are PHP persistent connections evil ?</a></li>\n<li><a href="http://us2.php.net/manual/en/features.persistent-connections.php" rel="nofollow noreferrer">Persistent Database Connections</a></li>\n</ul>\n\n<p>It is also worth noting that the newer mysqli extension does not even include the option to use persistent database connections.</p>\n\n<p>I\'m still using persitent connections at the moment but plan to switch to non-persistent in the near future.</p>\n'}, {'answer_id': 278841, 'author': 'dkretz', 'author_id': 31641, 'author_profile': 'https://Stackoverflow.com/users/31641', 'pm_score': 1, 'selected': False, 'text': "<p>In general, you'll need to use non-persistent connections sometimes, and it's nice to have a single pattern to apply to db connection design (as long as there's relatively little upside to using persistent connections in your context.)</p>\n"}, {'answer_id': 3978156, 'author': 'LionHeart', 'author_id': 250027, 'author_profile': 'https://Stackoverflow.com/users/250027', 'pm_score': 4, 'selected': False, 'text': '<p>In brief, my experience says that persistent connections should be avoided as far as possible.</p>\n\n<p>Note that mysql_close is a no-operation (no-op) for connections that are created using mysql_pconnect. This means persistent connection cannot be closed by client at will. Such connection will be closed by mysqldb server when no activity occurs on the connection for duration more than <strong>wait_timeout</strong>. If <strong>wait_timeout</strong> is large value (say 30 min) then mysql db server can easily reach <strong>max_connections</strong> limit. In such case, mysql db will not accept any future connection request. <em>This is when your pager starts beeping.</em></p>\n\n<p>In order to avoid reaching <strong>max_connections</strong> limit, use of Persistent connection need careful balancing of following variables...</p>\n\n<ol>\n<li>Number of apache processes on one host</li>\n<li>Total number of hosts running apache</li>\n<li>wait_timout variable in mysql db server</li>\n<li>max_connections variable in mysql db server</li>\n<li>Number of requests served by one apache process before it is re-spawned</li>\n</ol>\n\n<p>So, pl use persistent connection after enough deliberation. You may not want to invite complex runtime issues for a small gain that you get from persistent connection.</p>\n'}]
['https://Stackoverflow.com/questions/50303', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1344/']