title: mysql reconnect c++
body: <p>Right now I have a C++ client application that uses mysql.h to connect to a MYSQL database and have to preform some logic in case there is a disconnect. I'm wondering if this is the best way to reconnect to a MYSQL database in a situation where my client gets disconnected.</p>
  
  <pre><code>bool MYSQL::Reconnect(const char *host, const char *user, const char *passwd, const char *db)
  {
  bool out = false;
  
  pid_t command_pid = fork();
  if (command_pid == 0)
  {
      while(1)
      {
          sleep(1);
          if (mysql_real_connect(&amp;m_mysql, host, user, passwd, db, 0, NULL, 0) == NULL )
          {
              fprintf(stderr, "Failed to connect to database: Error: %s\n",
              mysql_error(&amp;m_mysql));
          }
          else
          {
              m_connected = true;
              out = true;
              break;
          }
      }
      exit(0);
  }
  
  if (command_pid &lt; 0)
      fprintf(stderr, "Could not fork process[reconnect]: %s\n", mysql_error(&amp;m_mysql));
  
  return out;
  }
  </code></pre>
  
  <p>Right now i take in all my parameters and preform a fork.  the child process attempts to reconnect every second with a sleep() statement.  Is this a good way to do this? Thanks</p>