title: How to assure that DB (Postgres) connection is still alive between prepare and execute?
body: <p>I have daemon script which runs forever in while loop. I have a prepared statement and this statement is executed on every loop.</p>
  
  <p>Example:</p>
  
  <pre><code>  my $dbh;
    sub get_dbh {
        return DBI-&gt;connect(...);
    }
  
    my $dbh = get_dbh();
    my $sth = $dbh-&gt;prepare("SELECT ....") or die $DBI::errstr;
  
    while (1) {
        // is connection still there??
        //if (!$dbh-&gt;ping) {
        //    $dbh = get_dbh();
        //}
  
        $sth-&gt;execute('param1');
  
        // do some other things ... sleep between 0 and 3600
    }
  </code></pre>
  
  <p>Problem occurs (or might occur) if prepared statement is prepared a few hours ago. Connection could die and my execute too. Checking $dbh->ping before every execute looks like an overkill.</p>
  
  <p>MySQL supports mysql_auto_reconnect which works really. DBD::Pg doesn't have something like that. I read about DBI::Apache but as I see it depends on mod_perl etc. It is obviously meant for web applications.</p>
  
  <p>Is there a "best-practice" way to do check for connection state and reconnect if needed?#</p>
  
  <p>I could prepare statement on every loop but that is not the solution but just a way around the problem.</p>