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?










share|improve this question
























  • 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















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?










share|improve this question
























  • 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













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?










share|improve this question















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 go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings