Doubt about web application [SOLVED]

Hey there, I got a doubt about database conection in a web application. Do I need to stablish one connection per request, or I can use the same database link to all the requests? Example below;

The db var below is declared global. Can I use this variable inside all functions and to all requests?

func login(w http.ResponseWriter, r *http.Request) {

	if !RestVerificaChave(r.URL.Query().Get("chave")) {
		fmt.Fprintf(w, "-1")
		return
	}

	fmt.Fprintf(w, "TESTE PAGINA WEB LOGIN EM GO-LANG")
}

func teste(w http.ResponseWriter, r *http.Request) {

	fmt.Fprintf(w, "TESTE PAGINA WEB TESTE EM GO-LANG")
}

func main() {

	// carrega configuracoes iniciais e conecta no banco ---------------------------

	LoadConfig()
	db = ConectaBanco()

	// carrega configuracoes iniciais e conecta no banco ---------------------------

	//chamada para os métodos ------------------------------------------------------

	http.HandleFunc(URLRaiz+"login/", login)
	http.HandleFunc(URLRaiz+"teste/", teste)

	//chamada para os métodos ------------------------------------------------------

	http.ListenAndServe(":81", nil)
}

Or for example, I gotta do like;

func login(w http.ResponseWriter, r *http.Request) {

	if !RestVerificaChave(r.URL.Query().Get("chave")) {
		fmt.Fprintf(w, "-1")
		return
	}

        //connection here inside the request
	db = ConectaBanco()

	fmt.Fprintf(w, "TESTE PAGINA WEB LOGIN EM GO-LANG")
}

func teste(w http.ResponseWriter, r *http.Request) {
        //connection here inside the request
	db = ConectaBanco()

	fmt.Fprintf(w, "TESTE PAGINA WEB TESTE EM GO-LANG")
}

func main() {

	LoadConfig()

	//chamada para os métodos ------------------------------------------------------

	http.HandleFunc(URLRaiz+"login/", login)
	http.HandleFunc(URLRaiz+"teste/", teste)

	//chamada para os métodos ------------------------------------------------------

	http.ListenAndServe(":81", nil)
}

Thanks.

I think you should reuse the connection. Opening and closing connections is expensive and should be minimised.

Why does your function ConectaBanco() do?

1 Like

Have one db that you open at the start, and pass around as a variable (global to data access package, or on the handler, or on the context passed to handlers, whatever you prefer). The database drivers usually manage a pool of connections for you transparently, so you don’t have to worry about that but you don’t want to be opening and closing the db for every request.

2 Likes

Nice, that was my doubt, thanks guys! My doubt was because I do web using PHP, and a db connection is opened and closed on every request, but in GO I saw I can open only one connection atg the main method and use it everywhere. Tnx.

but in GO I saw I can open only one connection atg the main method and use it everywhere

I think of open more like configuring a connection manager that uses a pool of connections to efficiently handle your queries. You (mostly) don’t have to worry about the connections themselves, just the queries.

In fact, sql.Open does not connect to the database until a connection is actually needed (e.g., for a query or ping).

2 Likes

It should connect to the database, something like: ConnectDB()

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.