TCP connection management to DB (mongodb)

227 views
Skip to first unread message

bsr

unread,
Feb 11, 2011, 2:10:35 PM2/11/11
to golang-nuts
Hello,
I am trying to use mongodb with gomongo driver, and not sure how to
handle the database connection. To connect to the database, it uses

net.DialTCP("tcp", nil, addr)

and there is a Disconnect() method, which closes the connection.

I never worked at this level , and wonder whether do I need to
establish the connection each time I request to the database. Please
give some pointers to what all I should look for (connection timeout,
limit etc).

thank you,
bsr

Fabio Kaminski

unread,
Feb 11, 2011, 4:49:14 PM2/11/11
to bsr, golang-nuts
dont know about gomongo.. but using go-mongo: github.com/garyburd/go-mongo 
is there a connection pool already coded is this version of the driver.. 

using as a open/close:

c, err := mongo.Dial(address)
defer c.Close()

or pooled:

at server start:
pool := mongo.NewPool(mongo.Dial, 0)

getting the connection at http request:

c, err := pool.Get()

and when your server were shuting down.. 

pool.Close()..

if you wanna use the gomongo.. then i think you can just adapt this code github.com/garyburd/go-mongo/pool.go
to that driver or.. open to go/doc/codewalk/urlpoll.go for a fancy example of polling using channels.. (the same used by go-mongo/pool.go) 

Fabio Kaminski

unread,
Feb 11, 2011, 4:51:14 PM2/11/11
to bsr, golang-nuts
Reply all
Reply to author
Forward
0 new messages