Golang sql database open and close -


in go database/sql package says rare close database db.close because meant shared many go routines. 1 better when given 100 functions queries same data:

  1. open database inside each function
  2. open database 1 time , use same connection every 100 function.

1 easier because if 1 fails other 99 can still working. , no need pass database connection arguments. in performance wise 1 better?

you missed important part of documentation says:

the returned db safe concurrent use multiple goroutines , maintains own pool of idle connections. thus, open function should called once. necessary close db.

(emphasis mine)

so, option #2 doesn't make sense. connections pooled - use same connection every 100 function doesn't apply. also, option #1 waste of time - once documentation states, call ping after make sure fine (and have attempt connect database - regardless of driver).


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -