I have a web application that I'm starting with. It works well at startup, but if I leave it (say, an hour) and hit it with another request, the request freezes. I thought about closing it after each request, and then opening a new connection, but the documents explicitly say: "It is rare to close the database, since the database descriptor must be durable and shared between many goroutines." What am I doing wrong?
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(&pk); err != nil { log.Println(err) } log.Println(pk) } log.Println("Request Served...") }
EDIT # 1: In my postgres log:
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
user776942
source share