Created
August 29, 2012 20:45
-
-
Save alexanderjulo/3518698 to your computer and use it in GitHub Desktop.
sqlalchemy mysql cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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