Skip to content

Commit

Permalink
Close db connections opened by exception views
Browse files Browse the repository at this point in the history
Make sure that db connections opened by exception views or by NewRequest
callbacks get closed.

Fixes #3683.
  • Loading branch information
seanh committed Aug 12, 2016
1 parent 79821ae commit 06a2f05
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions h/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ def _session(request):
else:
zope.sqlalchemy.register(session, transaction_manager=tm)

# pyramid_tm doesn't always close the database session for us.
#
# For example if an exception view accesses the session and causes a new
# transaction to be opened, pyramid_tm won't close this connection because
# pyramid_tm's transaction has already ended before exception views are
# executed.
# Connections opened by NewResponse and finished callbacks aren't closed by
# pyramid_tm either.
#
# So add our own callback here to make sure db sessions are always closed.
#
# See: https://github.com/Pylons/pyramid_tm/issues/40
@request.add_finished_callback
def close_the_sqlalchemy_session(request):
session.close()

return session


Expand Down

0 comments on commit 06a2f05

Please sign in to comment.