Optimal way of exposing Database connection in web applications(Golang)

mourya venkat
3 min readSep 7, 2019

--

So recently when developing my web application I’ve got to know what’s the true importance of private and public variables.

I’ve created a dummy web app to just showcase the use case. I am using Golang GIN Framework for my web app.

This is my main file where I call PointToDatabase which establishes the connection to database and sets the connection to the global variable. It exposes two routes which are handled by route handlers whose responsibility is to server incoming request with some response.

If you can see, during my server startup I am calling PointToDatabase which actually connects to the database and set the connection object to SQLConn which is of type *sql.DB.

Now that we have connection and server started, lets look at our route handlers.

What the code does is simply makes an sql query to fetch userDetails if the name sent in request matched the database value, it then sends back the response of user’s email and mobile.

If you closely observe the code, every time this function is called it gets the sqlConnection object by directly accessing the object.

This might not look like an issue right away, but hold for a second till I show you this code.

Lets look at the other route Handler.

If you look at the code closely, by mistake I’ve set the connection.SQLConn to nil.

The real problem starts from now. The current request will pass , but from now all the requests that uses this connection object start failing with a nil pointer exception.

The reason being we mistakenly updated the global variable which tend to apply on all requests.

So, how to we solve this problem.???

Here comes the concept of middleware and request level scoping.

The concept of request level scoping is that, instead of exposing the connection globally, make it available as a request Parameter so that whatever modification done will only affect that specific request instead of effecting the application.

Here is how the code looks like.

Now instead of calling the function to establish the connection and set it to global variable, I am using it as a middleware which attaches the sql Connection Object to the request Context.

If you look at the code, the function accepts a parameter of type *gin.Context which contains all the request data and set it to the context( scope lasts only till that request ends)

If you look into the code, instead of taking the sql Connection Object from global variable, it instead picks from request data (c.Get(“sqlconnection”)) and performs its operations.

The main advantage with this approach is your function will not be disturbed by others changing the connection Object. Its isolated and if modifications made to the SQL connection, it only affects the specific request instead of affecting the entire application.

Never expose Database connections globally throughout the application. Middlewares do exist for a good reason.

--

--

mourya venkat
mourya venkat

Written by mourya venkat

I will starve to death if you don’t feed me some code. Quora : https://www.quora.com/profile/Mourya-Venkat-1

No responses yet