Hi all,
I'd like to use SQLAlchemy with Tornado and I'd like to have some comments from whoever used it and is experienced with it.
Basically, what I want to avoid is making stupid mistakes like leaving open connections, open transactions, etc.
I figured out that something like these might work (my app is single threaded and no async calls are used, so really trivial).
class Application(tornado.web.Application):
def __init__(self, auth_handlers=None):
handlers = (
... list handlers here...
)
settings = {
... settings here...
}
tornado.web.Application.__init__(self, handlers, **settings)
engine = create_engine(dbname)
self.db = scoped_session(sessionmaker(bind=engine))
Then, each handler looks like this:
class MyHandler(tornado.web.RequestHandler):
def get(self):
... do something with self.application.db ...
self.application.db.commit()
def on_finish(self):
self.application.db.rollback() # rollback whatever was not committed explicitely
What do you think?
Would this work? Is it the right way of using sqlalchemy sessions?
Thanks,
L.