-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Question on pooling/concurrency regarding recent documentation change, suggest clarification #1053
Comments
@pjebs ...you might know the answer to this. |
What I don't understand specifically is that, using the global variable model, HTTP requests aren't passed into the DB wrapper. So, for database transactions, it seems like the requests could hit via multiple connections? Or does a transaction object save up all of the transactions and somehow fire them on a single connection? |
What's confusing in the comments in the documentation for
... It sounds like calling GORM DB's Then, the SQL driver says:
So, do we call it once per request, once per project (if a single global DB variable hasn't been initialized yet)... or what? |
GORM really has nothing to do with the underlying The advantage of that approach is that I can use the Eitherway, there are no concurrency issues. Multiple goroutines can use it safely. Now regarding best way to use it in a webserver app: go-sql-driver/mysql#461 |
OK, got it. This could definitely be clarified in the docs... so, I'll leave the issue open. I’m hoping to try and go back and add whatever I can to the docs to make it better based on the questions I’ve had to ask or research. |
@soldiershen That's what the documentation implies, but I haven't really done any heavy testing personally. |
@benguild I will test it.Is there a easy way to query live DB connection count on linux? I am not familiar with linux.Thanks |
Mysql command:
|
@pjebs |
No. Connection Pooling means you don't call You will need to set the |
Avoid error: panic: pq: sorry, too many clients already References: - https://github.com/jinzhu/gorm/issues/1427 - https://github.com/jinzhu/gorm/issues/1053
Myself and others were sort of unclear on how to correctly handle database connection pooling following more recent documentation changes. On older versions of the documentation, this phrasing was included:
GORM
has since been updated, and this phrasing has been removed from the documentation, as has examples of using a global variable to store a single*gorm.DB
object for the entire project at runtime.I personally opted to open a
*gorm.DB
object for each HTTP request, and hope that these connections were recycled from the connection pool in the SQL driver. From examining the code, this seems like the correct approach, but I wasn't sure ifGORM
handled this for us. It looks like by using a global variable like before, GORM would share a single database connection for every single request to the application. Maybe there's something I'm missing, but can someone clarify?Related: #246
The text was updated successfully, but these errors were encountered: