title: Too many connections with PDO persistent connection?
body: <p>I've been struggling with this for quite a while, and it's to the point where I need to ask for help because even with all the research I've done I can't get a handle on why this is happening.</p>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1040] Too many connections'</p>
  </blockquote>
  
  <p>This happens upon loading a single page (<code>index.php</code>), and I am the only user (dev). As you can see here, the MySQL connection limit is set @ 50 but I am narrowly surpassing that. This is an improvement over the 100~ connections that were being created before I refactored the code. </p>
  
  <p><a href="https://i.stack.imgur.com/EckLb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EckLb.png" alt="MySQL connections before"></a></p>
  
  <p>Here are the stats after the page has loaded once.</p>
  
  <p><a href="https://i.stack.imgur.com/6wItE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6wItE.png" alt="MySQL connections after"></a></p>
  
  <p>I've narrowed the issue down to several causes:</p>
  
  <ul>
  <li>I don't fully understand how PDO/MySQL connections work.</li>
  <li>I am creating too many connections in my code even though I am trying to only create one that I can share.</li>
  <li>I need to increase the connection limit (seems unlikely).</li>
  </ul>
  
  <p>Most of the SO questions I've found, tell the OP to increase the connection limit without truly knowing if that's the best solution so I'm trying to avoid that here if it's not needed. 50 connections for one page load seems like way too many.</p>
  
  <p>These are the classes I am instantiating on the page in question.</p>
  
  <pre><code>$DataAccess = new \App\Utility\DataAccess();
  $DataCopyController = new App\Controllers\DataCopyController($DataAccess);
  $DriveController = new App\Controllers\DriveController($DataAccess);
  $Helper = new App\Utility\Helper();
  $View = new App\Views\View();
  </code></pre>
  
  <p>I am creating the DAL object then injecting it into the classes that need it. By doing it this way I was hoping to only create one object and one connection, however this is not what's happening obviously. Inside the DAL class I've also added <code>$this-&gt;DbConnect-&gt;close()</code> to each and every query method.</p>
  
  <p>Here is the constructor for the <code>DataAccess()</code> class.</p>
  
  <pre><code>public function __construct() {
  
      $this-&gt;DbConnect = new \App\Services\DbConnect();
      $this-&gt;db = $this-&gt;DbConnect-&gt;connect("read");
      $this-&gt;dbmod = $this-&gt;DbConnect-&gt;connect("write");
  
      $this-&gt;Helper = new Helper();
  }
  </code></pre>
  
  <p>Here is the <code>DbConnect()</code> class.</p>
  
  <p>class DbConnect {</p>
  
  <pre><code>  private $db;
    private $dbmod;
  
    private function isConnected($connection) {
      return ($connection) ? TRUE : FALSE;
    } 
  
    public function connect($access) {
  
      $options = [
                  PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION,
                  PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC,
                  PDO::ATTR_EMULATE_PREPARES =&gt; false
                  ];
  
      if ($access == "read") {
          if ($this-&gt;isConnected($this-&gt;db)) {
              return $this-&gt;db;
          } else {
              if (strpos($_SERVER['SERVER_NAME'], DBNAME_DEV) === false) {
                  $this-&gt;db = new PDO("mysql:host=127.0.0.1; dbname=".DBNAME, 
                                                                      DBUSER, 
                                                                      DBPASS, 
                                                                      $options
                                                                         );
              } else {
                  $this-&gt;db = new PDO("mysql:host=" . DBHOST_DEV ."; dbname=".DBNAME_DEV, 
                                                                     DBUSER, 
                                                                     DBPASS, 
                                                                     $options
                                                                         );
              }
              return $this-&gt;db;
          }
      } elseif ($access == "write") {
          if ($this-&gt;isConnected($this-&gt;dbmod)) {
              return $this-&gt;dbmod;
          } else {
              if (strpos($_SERVER['SERVER_NAME'], DBNAME_DEV) === false) {
                  $this-&gt;dbmod = new PDO("mysql:host=127.0.0.1; dbname=".DBNAME, 
                                                                         DBUSER_MOD, 
                                                                         DBPASS, 
                                                                         $options
                                                                         );
              } else {
                  $this-&gt;dbmod = new PDO("mysql:host=" . DBHOST_DEV . "; dbname=".DBNAME_DEV, 
                                                                         DBUSER_MOD, 
                                                                         DBPASS, 
                                                                         $options
                                                                         );
              }
          }
          return $this-&gt;dbmod;
      }
    }
  
    public function close() {
      $this-&gt;db = null;
      $this-&gt;dbmod = null;
    }
  }
  </code></pre>
  
  <p>I've also tried instantiating the <code>DbConnect()</code> class on <code>index.php</code> and injecting that rather than <code>DataAccess()</code> but the result was the same.</p>
  
  <p><strong>EDIT:</strong>
  I also want to add that this MySQL server has two databases, prod and dev. I suppose the connection limit is shared between both. However, the prod database gets very little traffic and I am not seeing this error there. When I refreshed the stats, there were no connections to the prod database.</p>