Skip to content

Instantly share code, notes, and snippets.

@alexanderjulo
Created August 29, 2012 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderjulo/3518698 to your computer and use it in GitHub Desktop.
Save alexanderjulo/3518698 to your computer and use it in GitHub Desktop.
sqlalchemy mysql cleanup
from sqlalchemy import exc
from sqlalchemy import event
from sqlalchemy.pool import Pool
@event.listens_for(Pool, "checkout")
def ping_connection(dbapi_connection, connection_record, connection_proxy):
cursor = dbapi_connection.cursor()
try:
cursor.execute("SELECT 1")
except:
# optional - dispose the whole pool
# instead of invalidating one at a time
# connection_proxy._pool.dispose()
# raise DisconnectionError - pool will try
# connecting again up to three times before raising.
raise exc.DisconnectionError()
cursor.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment