Skip to content
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

Closed
samgavinio opened this issue Apr 9, 2017 · 15 comments
Closed

[Question] Do you need to close the db connection every request? #1427

samgavinio opened this issue Apr 9, 2017 · 15 comments

Comments

@samgavinio
Copy link

samgavinio commented Apr 9, 2017

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?

@samgavinio
Copy link
Author

Found a sufficiently good answer here: https://github.com/jinzhu/gorm/issues/1053

@ghost
Copy link

ghost commented Apr 11, 2017

@samgavinio Can you quickly summarize? Is it okay to Open a connection and use it everywhere else in the app and defer db.Close() it? Thanks!

@sgon00
Copy link

sgon00 commented May 18, 2017

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 defer db.Close() way which is opening and closing a connection in every request. I think opening and closing DB connection takes a lot of time and resources, but it seems gorm recommends this way.

@samgavinio I am wondering what your final decision is? thanks.

@stretchkennedy
Copy link

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, gorm.Open() uses https://golang.org/pkg/database/sql/, which is threadsafe and handles connection pooling for you. Don't call gorm.Open() every request. Call it once when setting up your application, and make sure you use defer db.Close() so connections are cleanly ended when your program exits.

@QianYu92
Copy link

QianYu92 commented May 9, 2018

But here: http://go-database-sql.org/retrieving.html it says if you don't Close a Query connection it would keep as open so you should defer rows.Close() it. How should we do here?

@g10guang
Copy link

@stretchkennedy OS wil automatically clean any socket connection after process exit. So I think defer db.Close() is not needed.

@stretchkennedy
Copy link

@g10guang db.Close() can do a lot more than just cleaning up socket connections. For example, the go-sqlite3 driver calls sqlite3_close_v2, which does a lot of things including running fsync and getting rid of temporary journal and wal files.

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.

@g10guang
Copy link

g10guang commented Nov 2, 2018

@stretchkennedy Thanks.😄

@Overover1400
Copy link

what will happen if we don't use "db.close" or " defer db.close"?(it's will close auto?)

@bjm88
Copy link

bjm88 commented Nov 10, 2018

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.

@cyberience
Copy link

@stretchkennedy OS wil automatically clean any socket connection after process exit. So I think defer db.Close() is not needed.

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.

@YudiTjhia
Copy link

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.

@spinales
Copy link

could you show me an example of how to implement it

@febrianrendak
Copy link

febrianrendak commented Jul 4, 2020

Found a sufficiently good answer here: https://github.com/jinzhu/gorm/issues/1053

the link is dead, can someone tell me the correct link or just paste here the solution/answer for this issue?

@stretchkennedy
Copy link

@febrianrendak It's issue #1053 in this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests