Skip to content

Commit

Permalink
close sqla session on database/sqlalchemy Pylons#52
Browse files Browse the repository at this point in the history
  • Loading branch information
kusut committed Jan 21, 2013
1 parent 022a6cd commit bea91c4
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion database/sqlalchemy.rst
Expand Up @@ -29,6 +29,12 @@ add a subclass of :class:`pyramid.request.Request` to it.
class MyRequest(Request):
@reify
def db(self):
# close the session when the request is finished
def cleanup(self):
self.db.close()
self.add_finished_callback(cleanup)
maker = self.registry.settings['db.sessionmaker']
return maker()
Expand All @@ -51,10 +57,10 @@ You can then use MyRequest as a request factory within your
from myapp.request import MyRequest
def main(global_config, **settings):
config = Configurator(settings=settings, request_factory=MyRequest)
engine = engine_from_config(settings, prefix='sqlalchemy.')
maker = sessionmaker(bind=engine)
settings['db.sessionmaker'] = maker
config = Configurator(settings=settings, request_factory=MyRequest)
# .. rest of configuration ...
The db connection is now available in view code as ``request.db`` or
Expand Down

0 comments on commit bea91c4

Please sign in to comment.