The usual pattern is to have a global *sql.Db object obtained via calling 
the Open() function as explained at [1]. You don't have to worry about 
maintaining the connection pool yourselves as that's automatically done. 
The Go tutorial on the topic [2] shows you can do this using a global 
variable. You can configure various properties of the pool via functions 
such as SetMaxOpenCons [3] amd SetConnMaxIdleTime[4].

If you are using a struct for managing the other configuration in your 
application, you would have the *sql.Db object as a field in that struct 
and then share that struct value with all the parts of your application 
that needs access to it.

[1] https://pkg.go.dev/database/sql#Open
[2] https://go.dev/doc/tutorial/database-access
[3] https://pkg.go.dev/database/sql#DB.SetMaxOpenConns
[4] https://pkg.go.dev/database/sql#DB.SetConnMaxIdleTime

On Sunday, January 9, 2022 at 1:29:07 AM UTC+11 Shishira Pradhan wrote:

> Hello All,
>
> What is the appropriate and industry standard mechanism to send database 
> connection across golang file to use in logic layer to get data from DB? It 
> should be one time connection established and many time use the connection 
> pool across all files.
>
> Thanks,
> Shishira
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c2c8c729-268f-47c8-b700-9c38ef894f88n%40googlegroups.com.

Reply via email to