I have a service that I wrote that does a few simple things atm. We have a web app that has a list of menus across the top of the page. Instead of accessing the database directly in php I decided to use it as a test to work with go.
I have a global db variable that holds the connection to the db.
I have an init function that sets up the connection as well as creates some db statements to use later on.
I have a main function that sets up the listeners and waits for connections.
The db connection is shared across all incoming connections which I thought was wrong but was told it was the correct way to do it. Is this true or should I be creating new connections within each inbound request?
If I should be creating new connections in each call then how do I work with the prepared statements? They seem only be able to be created with an active db connection.
My main issue is that if I let the service sit for a few minutes it will report that it has lost its db connection. It then seems to recreate it and starts to work but if not kept active it will lose its bearings and ends up sending back errors instead of my list of menus.
I’m trying to get my head wrapped around the whole db and how to manage it with go because currently things appear to be inconsistent with go.