Skip to content

global connection for database #29

Closed
karpitsky opened this Issue Feb 20, 2012 · 3 comments

4 participants

@karpitsky

Hello,

I can in tornado make global connect to mongo ( self.db in tornado.web.Application ) and use as self.application.db in handlers ( with tornado.web.RequestHandler)

How can I do this in TornadIO2?

@mrjoes
Owner
mrjoes commented Feb 28, 2012

Right now, application instance is not passed to the session/connection class. I will think how to do it.

@etchalon

I figured out a cheap way to do this, by passing the db connection through the user_settings dict of the router.

I'm using Motor, but the same should apply for AsyncMongo.

db = motor.MotorConnection('localhost', 27017).open_sync().database
router   = tornadio2.TornadioRouter(QueryRouter, user_settings={'db':db})
sock_app = tornado.web.Application(router.urls, ...

Then, in my SocketConnection subclass:

@property
def mongo(self):
    if not hasattr(self, '_mongo'):
        self._mongo = self.session.server.settings['db']
    return self._mongo

Not the greatest way, but it works.

@mrjoes mrjoes closed this Feb 19, 2013
@lessandro

Alternatively, you can pass a function with a closure that creates SocketConnection objects. (e.g. https://github.com/lessandro/py-ircd/blob/master/src/net_sio.py#L34)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.