Guest User

Untitled

a guest
Mar 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. ##### Initialize elixir/SQLAlchemy
  2. # Disconnect handling
  3. from sqlalchemy import exc
  4. from sqlalchemy import event
  5. from sqlalchemy.pool import Pool
  6.  
  7. @event.listens_for(Pool, "checkout")
  8. def ping_connection(dbapi_connection, connection_record, connection_proxy):
  9.     logging.debug("***********ping_connection**************")
  10.     cursor = dbapi_connection.cursor()
  11.     try:
  12.         cursor.execute("SELECT 1")
  13.     except:
  14.         logging.debug("######## DISCONNECTION ERROR #########")            
  15.         # optional - dispose the whole pool
  16.         # instead of invalidating one at a time
  17.         # connection_proxy._pool.dispose()
  18.  
  19.         # raise DisconnectionError - pool will try
  20.         # connecting again up to three times before raising.
  21.         raise exc.DisconnectionError()
  22.     cursor.close()
  23.  
  24. metadata.bind= create_engine("mysql://foo:bar@localhost/db_name", pool_size=100, pool_recycle=3600)
  25.  
  26. setup_all()
Add Comment
Please, Sign In to add comment