title: Store database (PDO) connection
body: <p>I'm currently writing a PHP application and i noticed that my page loads kinda slow. I takes about 2 seconds (2.0515811443329 to be exact).</p>
  
  <p>I've tracked down what the bottleneck was and it's the part where i'm creating a PDO connection to my MySQL database.</p>
  
  <p>My 'connect()' method doesn't do any exoctic stuff. It simply looks like this:</p>
  
  <pre><code>public function connect ( $database, $host, $username, $password )
  {
      try
      {
          $this-&gt;db = new \PDO("mysql:dbname=".$database.";host=".$host, $username, $password);
          if ( !$this-&gt;db )
          {
              throw new \Exception('Failed to connect to the database!');
          }
  
          $this-&gt;db-&gt;setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
      }
      catch ( \Exception $e )
      {
          echo '&lt;strong&gt;Exception: &lt;/strong&gt;'.$e-&gt;getMessage();
          return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p>So when i comment out the call to the 'connect()' method, then my page loads in: 0.035506010055542</p>
  
  <p>This is a huge difference. I can imagine that creating a connection to a database does take up some time, but it takes more than 1,5 seconds... I'm not sure if this is normal?</p>
  
  <p>If it is normal, that it takes up that amount of time then is there a way to store the database connection? Like putting it in a session? Actually, as far as i know storing it in a session isn't possible. But it would be the ideal solution. Storing the connection somewhere until the user closes his browser.</p>
  
  <p>In anyway, is there a problem with my PDO / MySQL?  And can i simply store the connection resource somehow? So that i don't have to reconnect to my database everytime for every new page?</p>
  
  <p>PS. I'm doing this all on a localhost (Windows).</p>