Mysql Connections

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.

  1. 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?

  2. 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.

  3. 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.

Thanks,

Glenn

The sessions are reused automatically in Go by the pool. You can control this behavior and set the session length etc: http://go-database-sql.org/connection-pool.html

Is this what you are looking for?

Well, at the moment I added db.SetMaxIdleConns(0) to init function and that seems to have eliminated the losing of the db connection. Still testing to see how stable it is.

Thanks,

Glenn

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