How to keep sql.DB open
up vote
-2
down vote
favorite
I'm trying to build a micro service that connects to a database and serves json based on the endpoint that are hit.
func main() {
mux := httprouter.New()
mux.GET("/", index)
mux.GET("/adminUser", adminUserReturn)
http.ListenAndServe(":8080", mux)
}
func adminUserReturn(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
user := "homestead"
pass := "secret"
dbname := "test-db"
dbHost := "127.0.0.1"
dbPort := "33060"
db, err := sql.Open("mysql", user+":"+pass+"@tcp("+dbHost+":"+dbPort+")/"+dbname)
.
.
.
.
defer db.Close()
}
My question is should I "open" a connection in each endpoint then "close" the connection when the method is done executing? Or should I just initiate the sql.DB "instance" in the main function and defer db.Close() in main as well? That way the db connection will never close (assuming the program keeps running). Could this lead to the DB being overloaded with connections?
mysql web
add a comment |
up vote
-2
down vote
favorite
I'm trying to build a micro service that connects to a database and serves json based on the endpoint that are hit.
func main() {
mux := httprouter.New()
mux.GET("/", index)
mux.GET("/adminUser", adminUserReturn)
http.ListenAndServe(":8080", mux)
}
func adminUserReturn(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
user := "homestead"
pass := "secret"
dbname := "test-db"
dbHost := "127.0.0.1"
dbPort := "33060"
db, err := sql.Open("mysql", user+":"+pass+"@tcp("+dbHost+":"+dbPort+")/"+dbname)
.
.
.
.
defer db.Close()
}
My question is should I "open" a connection in each endpoint then "close" the connection when the method is done executing? Or should I just initiate the sql.DB "instance" in the main function and defer db.Close() in main as well? That way the db connection will never close (assuming the program keeps running). Could this lead to the DB being overloaded with connections?
mysql web
golang.org/pkg/database/sql/#DB - it uses a connection pool. Use the library as recommended in the documentation.
– Adrian
Nov 7 at 21:04
1
@Flimzy thanks, that was the wrong link. Fixed comment: Do the second approach, one DB handle per app. It is the recommended approach and I believe it's mentioned in the documentation to Open or DB. Edit: read the last paragraph here: golang.org/pkg/database/sql/#Open
– mkopriva
Nov 7 at 21:49
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I'm trying to build a micro service that connects to a database and serves json based on the endpoint that are hit.
func main() {
mux := httprouter.New()
mux.GET("/", index)
mux.GET("/adminUser", adminUserReturn)
http.ListenAndServe(":8080", mux)
}
func adminUserReturn(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
user := "homestead"
pass := "secret"
dbname := "test-db"
dbHost := "127.0.0.1"
dbPort := "33060"
db, err := sql.Open("mysql", user+":"+pass+"@tcp("+dbHost+":"+dbPort+")/"+dbname)
.
.
.
.
defer db.Close()
}
My question is should I "open" a connection in each endpoint then "close" the connection when the method is done executing? Or should I just initiate the sql.DB "instance" in the main function and defer db.Close() in main as well? That way the db connection will never close (assuming the program keeps running). Could this lead to the DB being overloaded with connections?
mysql web
I'm trying to build a micro service that connects to a database and serves json based on the endpoint that are hit.
func main() {
mux := httprouter.New()
mux.GET("/", index)
mux.GET("/adminUser", adminUserReturn)
http.ListenAndServe(":8080", mux)
}
func adminUserReturn(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
user := "homestead"
pass := "secret"
dbname := "test-db"
dbHost := "127.0.0.1"
dbPort := "33060"
db, err := sql.Open("mysql", user+":"+pass+"@tcp("+dbHost+":"+dbPort+")/"+dbname)
.
.
.
.
defer db.Close()
}
My question is should I "open" a connection in each endpoint then "close" the connection when the method is done executing? Or should I just initiate the sql.DB "instance" in the main function and defer db.Close() in main as well? That way the db connection will never close (assuming the program keeps running). Could this lead to the DB being overloaded with connections?
mysql web
mysql web
edited Nov 7 at 20:32
Flimzy
36.4k96496
36.4k96496
asked Nov 7 at 20:06
David Jarrin
1621118
1621118
golang.org/pkg/database/sql/#DB - it uses a connection pool. Use the library as recommended in the documentation.
– Adrian
Nov 7 at 21:04
1
@Flimzy thanks, that was the wrong link. Fixed comment: Do the second approach, one DB handle per app. It is the recommended approach and I believe it's mentioned in the documentation to Open or DB. Edit: read the last paragraph here: golang.org/pkg/database/sql/#Open
– mkopriva
Nov 7 at 21:49
add a comment |
golang.org/pkg/database/sql/#DB - it uses a connection pool. Use the library as recommended in the documentation.
– Adrian
Nov 7 at 21:04
1
@Flimzy thanks, that was the wrong link. Fixed comment: Do the second approach, one DB handle per app. It is the recommended approach and I believe it's mentioned in the documentation to Open or DB. Edit: read the last paragraph here: golang.org/pkg/database/sql/#Open
– mkopriva
Nov 7 at 21:49
golang.org/pkg/database/sql/#DB - it uses a connection pool. Use the library as recommended in the documentation.
– Adrian
Nov 7 at 21:04
golang.org/pkg/database/sql/#DB - it uses a connection pool. Use the library as recommended in the documentation.
– Adrian
Nov 7 at 21:04
1
1
@Flimzy thanks, that was the wrong link. Fixed comment: Do the second approach, one DB handle per app. It is the recommended approach and I believe it's mentioned in the documentation to Open or DB. Edit: read the last paragraph here: golang.org/pkg/database/sql/#Open
– mkopriva
Nov 7 at 21:49
@Flimzy thanks, that was the wrong link. Fixed comment: Do the second approach, one DB handle per app. It is the recommended approach and I believe it's mentioned in the documentation to Open or DB. Edit: read the last paragraph here: golang.org/pkg/database/sql/#Open
– mkopriva
Nov 7 at 21:49
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197027%2fhow-to-keep-sql-db-open%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
golang.org/pkg/database/sql/#DB - it uses a connection pool. Use the library as recommended in the documentation.
– Adrian
Nov 7 at 21:04
1
@Flimzy thanks, that was the wrong link. Fixed comment: Do the second approach, one DB handle per app. It is the recommended approach and I believe it's mentioned in the documentation to Open or DB. Edit: read the last paragraph here: golang.org/pkg/database/sql/#Open
– mkopriva
Nov 7 at 21:49