title: MySQL, should I stay connected or connect when needed?
body: <p>I have been logging temperatures at home to a MySQL database (read 10 sensors in total every 5 minutes), and have been using Python, but I am wondering something...</p>
  
  <p>Currently when I first run my program, I run the normal connect to MySQL, which is only run once.</p>
  
  <pre><code>db = MySQLdb.connect(mysql_server, mysql_username, mysql_passwd, mysql_db)
  cursor = db.cursor()
  </code></pre>
  
  <p>Then I collect the data and publish it to the database successfully.  The script then sleeps for 5 minutes, then starts again and collects and publishes the data again and so on. However, I only connect once, and I don't ever disconnect; it just keeps going in a loop. I only disconnect if I terminate the program.</p>
  
  <p>Is this the best practice?  That is, keeping the connection open all the time to the MySQL server, or should I disconnect after I have done a insert/commit?</p>
  
  <p>The reason I ask: every now and then, I have to restart the script because maybe my MySQL server has gone offline or some other issue. Should I:</p>
  
  <ul>
  <li>Keep doing what I am doing and just handle any MySQL database disconnections with a reconnect,</li>
  <li>Put it in the crontab to collect data every five minutes and have no loop and no sleep, or</li>
  <li>Something else?</li>
  </ul>