title: Database hangs if not used
body: <p>I have a web application I am starting. Works fine upon startup but if I leave it (for say, an hour) and hit it with another request the query hangs. I thought about closing it after each query then opening up a new connection but the docs explicitly say "It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.". What am I doing wrong?</p>
  
  <pre><code>package main
  
  import (
    "database/sql"
    "log"
    "net/http"
    _ "github.com/lib/pq"
  )
  
  var Db *sql.DB
  
  func main() {
    var err error
    Db, err = sql.Open("postgres", "user=me password=openupitsme host=my.host.not.yours dbname=mydb sslmode=require")
    if err != nil {
      log.Fatal("Cannot connect to db: ", err)
    }
    http.HandleFunc("/page", myHandler)
    http.ListenAndServe(":8080", nil)
  }
  
  func myHandler(w http.ResponseWriter, r *http.Request) {
    log.Println("Handling Request....", r)
    query := `SELECT pk FROM mytable LIMIT 1`
    rows, err := Db.Query(query)
    if err != nil {
      log.Println(err)
    }
    defer rows.Close()
    for rows.Next() {
      var pk int64
      if err := rows.Scan(&amp;pk); err != nil {
        log.Println(err)
      }
      log.Println(pk)
    }
    log.Println("Request Served...")
  }
  </code></pre>
  
  <p>EDIT #1:
  My postgres log shows:</p>
  
  <pre><code>2015-07-08 18:10:01 EDT [7710-1] user@here LOG:  could not receive data from client: Connection reset by peer
  2015-07-08 18:20:01 EDT [7756-1] user@here LOG:  could not receive data from client: Connection reset by peer
  </code></pre>