title: perl dbi reconnect on disconnect
body: <p>I have searched google but could not find an answer to what I think is an easy question.</p>
  
  <p>I have a Perl code (example below) that gets data every 3 seconds and updates the received data into MySQL database but sometimes MySQL database is not available and the script dies. How can I make MySQL connection again if it fails?</p>
  
  <pre><code>use DBD::Mysql;
  
  sub updateMysqlDB{
  my $connect = DBI-&gt;connect("dbi:mysql:$database:$host", 
                          $user,
                          $pw,
                          {RaiseError =&gt; 1}
                          );
  $myquery = "My sql query to insrt data into columns";
  $query_handle=$connect-&gt;prepare($myquery);
  $query_handle-&gt;execute();
  $connect-&gt;disconnect;
  }
  
  while (1) {
  
  if data received call updateMysqlDB ();
  
  else wait for data { sleep 3 ;}
  }
  </code></pre>