-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sharing connections between goroutines #672
Comments
Hello, I've been using this library for a project for some time now, and currently I'm testing out the v4 version. This example is not correct, firstly the "Session ID: 00001" is not the connection number *sql.DB, and second to better simulate the workload, you can use mysql SLEEP() function:
then you can use tool "siege" https://github.com/JoeDog/siege to put pressure on the server and you will see in the stdout of the server that the pool is immediately utilized. e.g. showing that there is 10 OpenConnections and 10 InUse.
As for the things I personally didn't like in v4 are: The "sqlbuilder" package was marked as internal, and now when you want to access the interface "Tx" you have to do hacks like so:
The syntax for committing transactions is very limited. You can accomplish most of the work by using db.Session.Tx(func...) but sometimes you need direct access to control as the given example |
Hi,
We're building an API using
net/http
(server) andgithub.com/gorilla/mux
(router) and have some difficulties understanding how to best manage open connections in an efficient way usingupper/db
formysql
.I believe the underlying
sql/database
module manages a pool of connections that each goroutine can use when needed and then release it back to the pool for others to use. However, when running the following code in a stress test, we see some weird behavior.https://go.dev/doc/database/manage-connections
According to your documentation, we should start a session (open an actual connection) and then specify pool configuration details like
max open connections
,max idle connection
, andconnection max idle time
which seems a bit weird to do that on an open/active session (connection).After the session (connection) has been established, it's assigned to the global
DB
variable and used throughout the application including goroutines.The weird behavior is that under stress,
upper
sometimes log slow queries - which is fine. However, each log statement includes information about the session in use. All of our logs state that a given query was slow for the session with id00001
. Given that ourmax open connections
is set to10
, I would expect to see different session id's between00001
-00010
. It seems like the same session is always being used and not balanced between a pool of 10-15 connections.I've tried opening the connection (session) in each router handler (goroutine) but we don't want to open too many connections to the database.
I know that something is wrong with my code and my understanding, but I haven't been able to find any good answers, so... I hope someone can help!
The text was updated successfully, but these errors were encountered: