Database sessions for web applications

452 views
Skip to first unread message

jaybill

unread,
Apr 11, 2012, 4:32:07 PM4/11/12
to mgo-...@googlegroups.com
So I'm building a web application that uses mgo. When the app starts, I create a database session and cram a pointer to it in a "context" struct. I then pass a pointer to the context struct (which has other things in it, like the configuration data I loaded from a file, etc.) into my handlers. My logic here was that I didn't want to make a new db connection on every request.

This works, but I'm not sure if it's a good idea. The db session never gets closed and stays open (I presume) until I kill the app. (Calling close with "defer" closes the session before I get to use it.)

Is this the right strategy? If not, where's the right place to make database connections?

Thanks in advance!

Gustavo Niemeyer

unread,
Apr 11, 2012, 6:16:14 PM4/11/12
to mgo-...@googlegroups.com
Hey there,

It's fine to pick a new session whenever you please (session.Copy, for
example), and closing it when the logic is done with whatever was
being done (maybe handling a request). This will internally put the
connection used back into the pool for reuse on the next session.

If you're using only the base session rather than copying it, that's
fine as well for most applications where concurrency isn't necessary.
In those cases you can session.Refresh() at the end of the procedure
rather than session.Close(), so that the connection is put back into
the pool.

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog

-- I'm not absolutely sure of anything.

jaybill

unread,
Apr 11, 2012, 6:28:16 PM4/11/12
to mgo-...@googlegroups.com
Thanks so much for the quick reply! That answers my question perfectly!

--Jaybill


On Wednesday, April 11, 2012 3:16:14 PM UTC-7, Gustavo Niemeyer wrote:
Hey there,

nvcnvn

unread,
Apr 28, 2012, 8:32:18 AM4/28/12
to mgo-...@googlegroups.com
Forgive for my stupid, but...how do I copy the session?
Can some one give me an example with my code here:
_______________________________
type Hello struct {
session *mgo.Session
}

func (h Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello!")
//Get some data from mongo here
}

func main() {
var h Hello
http.ListenAndServe("localhost:4000",h)
}
_______________________________
Thanks you!
Vào 05:16:14 UTC+7 Thứ năm, ngày 12 tháng tư năm 2012, Gustavo Niemeyer đã viết:

nvcnvn

unread,
Apr 28, 2012, 8:28:52 PM4/28/12
to mgo-...@googlegroups.com
Is my code right:
_________________________________________________________
type Hello struct {
session *mgo.Session
}

func (h Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s := h.session.Copy()
}

func main() {
var h Hello
h.session, err := mgo.Dial("server1.example.com,server2.example.com")
if err != nil { panic(err) }
http.ListenAndServe("localhost:4000",h)
}
__________________________________________________________

Vào 05:16:14 UTC+7 Thứ năm, ngày 12 tháng tư năm 2012, Gustavo Niemeyer đã viết:
Hey there,

Gustavo Niemeyer

unread,
Apr 30, 2012, 8:30:23 PM4/30/12
to mgo-...@googlegroups.com
Heya,

> Is my code right:
(...)
> func (h Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {
> s := h.session.Copy()

Almost. You can either use h.session directly here, if you're happy
with a single connection to the database, or you have to call:

defer s.Close()

here, to avoid having this copied session leaking.
Reply all
Reply to author
Forward
0 new messages