Fix "Too Many Connections" in Golang

Sometimes people may come into Too Many Connections error when using Mysql in Golang. A few things should be noted to avoid this error.

  1. Remember to use DB.COMMIT() if transaction is used. Connections to Mysql are released only if the transaction is commited. As far as I concern, DB.Exec doesn't have this problem.

  2. Use Row.Close() after you do a query.

For example:

row, err := db.Query("select a from b limit 1")
if row != nil {
    defer row.Close()
}

row.Close() is very important, otherwise Golang will keep that connection.