title: PHP/PDO - Server has gone away and Exceptions
body: <p>I am attempting to make a php + pdo website that has persistent connections. Of course, the connections are closed every 8 hours of inactivity so I am attempting to create a php file that reopens the connection if it is closed</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    try{
              $db = new PDO("mysql:host=$database_ip;dbname=$database_name", $database_username, $database_password, array(PDO::ATTR_PERSISTENT =&gt; true)); 
      }catch(Exception $x){
              try{
                  $db = new PDO("mysql:host=$database_ip;dbname=$database_name", $database_username, $database_password);
              }catch(Exception $x){
                  echo 'Failed database error';
              }
  }
  </code></pre>
  
  <p><a href="http://www.php.net/manual/en/pdo.error-handling.php" rel="nofollow">This</a> says that "PDO::__construct() will always throw a PDOException if the connection fails regardless of which PDO::ATTR_ERRMODE is currently set. Uncaught Exceptions are fatal."</p>
  
  <p>The problem is that even though the exception is caught it is still fatal :/</p>
  
  <p>Here's the error:</p>
  
  <p><b>Warning</b>:  PDO::__construct(): MySQL server has gone away in <b>/path/to/website/mysql.inc.php</b> on line <b>3</b><br /></p>