title: Setting a connect timeout with PDO
body: <p>I'm using PDO to get data off a MySQL server. What I noticed is this: if the MySQL server is unavailable, it takes <em>really</em> (relatively) long for this code to return an exception:</p>
  
  <pre><code>try {
    $handle = new PDO($db_type . ':host='.$db_host.';dbname='.$db_name,$db_user,$db_pass);
    // Tried using PDO::setAttribute and PDO::ATTR_TIMEOUT here
  } catch(PDOException $e) {
    echo $e-&gt;getMessage;
  }
  </code></pre>
  
  <p>In case of MySQL it takes just over 2 minutes for the exception to occur (SQLSTATE[HY000] [2003] Can't connect to MySQL server on...) and 30 seconds on PostgreSQL (SQLSTATE[08006] [7] timeout expired).</p>
  
  <p>I tried using PDO::setAttribute and PDO::ATTR_TIMEOUT but it's not working. Which I guess makes sense, since the problem occurs before this statement.</p>
  
  <p>Is there a way to set a timeout for connecting to the DB? 2 minutes/30 seconds seems really long to me for PDO to realize there is nothing there.</p>
  
  <p>I think I saw this being done somewhere, but can't find it again for the life of me.</p>