My Pylons app uses local MySQL server via SQLAlchemy and python-MySQLdb. When the server is restarted, open pooled connections are apparently closed, but the application doe
You can use SQLAlchemy proxy for exception handling on each sql query:
from sqlalchemy.interfaces import ConnectionProxy
class MyProxy(ConnectionProxy):
def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
try:
return execute(cursor, statement, parameters, context)
except sqlalchemy.exc.OperationalError:
# Handle this exception
pass
To connect this proxy you must do that in config/enviroment.py
engine = engine_from_config(config, 'sqlalchemy.', proxy=MyProxy())
Or write middleware for exception handling on each http query:
class MyMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
try:
return self.app(environ, start_response)
except sqlalchemy.exc.OperationalError:
start_response(
'500 Internal Server Error',
[('content-type', 'text/html')])
return ['error page\n']
To connect this middleware in stack order as you need or simply in config/middleware.py:
# CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
app = MyMiddleware(app)