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:
- open database inside each function
- 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
Post a Comment