Cleanup sql session after request. ALWAYS!

The cleanup could be missed if the request handling code in
app.py:__call__ exits early (due to exception, or due to
one of those early "return"s).
So to make sure the sql session is cleaned up for real,
wrap the whole thing in a try: finally:.

Also wrote a short tool to test if the session is actually
empty. The tool is currently disabled, but ready to be
used.
This commit is contained in:
Elrond 2012-04-07 23:21:59 +02:00
parent abe7417827
commit e824570a23
2 changed files with 18 additions and 9 deletions

View File

@ -109,7 +109,7 @@ class MediaGoblinApp(object):
self.meddleware = [common.import_component(m)(self)
for m in meddleware.ENABLED_MEDDLEWARE]
def __call__(self, environ, start_response):
def call_backend(self, environ, start_response):
request = Request(environ)
## Routing / controller loading stuff
@ -184,16 +184,19 @@ class MediaGoblinApp(object):
for m in self.meddleware[::-1]:
m.process_response(request, response)
# Reset the sql session, so that the next request
# gets a fresh session
try:
self.db.reset_after_request()
except TypeError:
# We're still on mongo
pass
return response(environ, start_response)
def __call__(self, environ, start_response):
## If more errors happen that look like unclean sessions:
# self.db.check_session_clean()
try:
return self.call_backend(environ, start_response)
finally:
# Reset the sql session, so that the next request
# gets a fresh session
self.db.reset_after_request()
def paste_app_factory(global_config, **app_config):
configs = app_config['config'].split()

View File

@ -37,6 +37,12 @@ class DatabaseMaster(object):
Session.add(obj)
Session.flush()
def check_session_clean(self):
for dummy in Session():
_log.warn("STRANGE: There are elements in the sql session. "
"Please report this and help us track this down.")
break
def reset_after_request(self):
Session.rollback()
Session.remove()