-
-
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] Do you need to close the db connection every request? #1427
Comments
Found a sufficiently good answer here: https://github.com/jinzhu/gorm/issues/1053 |
@samgavinio Can you quickly summarize? Is it okay to Open a connection and use it everywhere else in the app and |
I had the same question and after reading 5 related issues, I am still confused right now and don't know which way is the best practice. The official gorm doc just shows @samgavinio I am wondering what your final decision is? thanks. |
For anyone who comes across this issue later on, the correct way to deal with database connections in a long-running application is something like func main() {
db, err := gorm.Open("sqlite3", "example.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// if your program has a main loop, start it here
// i.e. for a http server:
// http.ListenAndServe(":3000", func (w http.ResponseWriter, r *http.Request) {
// db.Find(...)
// })
} Behind the scenes, |
Avoid error: panic: pq: sorry, too many clients already References: - https://github.com/jinzhu/gorm/issues/1427 - https://github.com/jinzhu/gorm/issues/1053
But here: http://go-database-sql.org/retrieving.html it says if you don't |
@stretchkennedy OS wil automatically clean any socket connection after process exit. So I think |
@g10guang Even if running cleanup code is currently unnecessary, it's generally a bad idea to leave it out, because the next version of a library could change the implementation. |
@stretchkennedy Thanks.😄 |
what will happen if we don't use "db.close" or " defer db.close"?(it's will close auto?) |
you should call db.close for DB's sake as well, it is still maintaining a connection and will eventually time it out if no keep alive activity, but not a good practice to rely on that. |
Prevent data corruption if there is pending writes, or other cache based activities related to your query, this will flush and pending activity, including possible index updates. |
it looks like when you only use single open connection and the multi concurrent requests happen, the code with BeginTransaction will lock the single open connection, so the later requests will be locked waiting until former request get finished. correct me if wrong. |
could you show me an example of how to implement it |
the link is dead, can someone tell me the correct link or just paste here the solution/answer for this issue? |
@febrianrendak It's issue #1053 in this repo. |
Hi 👋, please label this github issue as a question.
Just recently got into labstack/echo with gorm as the ORM and I've noticed that the echo cookbooks out there generally show that you would clone a new connection and close it every request.
Looking at the gorm docs, there doesn't seem be anything specific about doing this and most of the stuff I've seen generally just open one persistent connection and pass it around.
Is there anything note worthy in doing either with gorm?
The text was updated successfully, but these errors were encountered: