Pylons/pyramid_zodbconn

the finished callback should only be added if request.tm is not defined

Opened this issue · 0 comments

If request.tm is defined, the events should be hooked into the transaction manager and emitted there, instead of at the end of the request. The current code assumes the connection is managed by repoze.tm in wsgi middleware and thus will not be closed yet and thus it can close itself.

I'm not positive on the inner details of how ZODB manages a connection after an abort, but if it closes it then this code should not close it or should guard against it already being closed when trying to close it.

def finished(request):
# closing the primary also closes any secondaries opened
registry.notify(ZODBConnectionWillClose(primary_conn, request))
try:
primary_conn.transaction_manager.abort()
except NoTransaction:
pass
primary_conn.close()
registry.notify(ZODBConnectionClosed(primary_conn, request))
request.add_finished_callback(finished)

This is more of an informational issue as I was reviewing the zodb cookiecutter and noticing that there's probably some oddities here but I don't plan to dive into them myself other than to point them out.