Golang Database Connection and Pools

718 views
Skip to first unread message

Joel Eric

unread,
Aug 12, 2014, 3:53:48 PM8/12/14
to golan...@googlegroups.com
What is the correct pattern for connecting to databases (in my case MongoDB) with GOLang web applications?  I am used to a Java Background where a connection pool is recommended and usually maintained by the application server.  Is this important in GO and if so how is it best implemented?  Conversely is it better to create a connection for each web request and close it at the end of the request OR should a database connection be held in a global variable?  Any information or links to best practices on this topic is much appreciated.  

Joel

Shawn Milochik

unread,
Aug 12, 2014, 3:57:11 PM8/12/14
to golan...@googlegroups.com
You automatically get a pool. 

Much more useful info in this good talk. There are a couple of potential gotchas.



Tamás Gulácsi

unread,
Aug 12, 2014, 4:05:50 PM8/12/14
to golan...@googlegroups.com
database/sql already does the pooling for you.

Ingo Oeser

unread,
Aug 12, 2014, 5:59:47 PM8/12/14
to golan...@googlegroups.com
For your usecase of MongoDB, please refer to the information at http://godoc.org/labix.org/v2/mgo#Dial which describes the pooling for it.

Fatih Arslan

unread,
Aug 12, 2014, 6:05:21 PM8/12/14
to golang-nuts
As the others already said, mgo (Go MongoDB driver) already does the pooling for you. However if you wish to use a custom connection pool in your webserver, here is a package that provides such a feature:

https://github.com/fatih/pool

Maybe it's not what you want, but at least reading the code would be helpful.

Regards

--
Fatih Arslan
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages