I'm working on an application where I want to use one and only one connection, even in the face of errors. Originally I used AssertionPool, but it seemed to misbehave sometimes in the face of a disconnection. Switching to QueuePool, I somewhat surprisingly got the same result.
This is how I perform my test. Using the code below, I start it with a URI and wait for it to tell me to stop postgresql, which I then do. I hit enter, and get an (expected) error as it fails to connect. When I hit enter a second time, I get an *unexpected* traceback, a different one for QueuePool as for AssertionPool, but unexpected nonetheless. If I up the pool_size to 2, everything works fine but it leaves open the possibility of having more than one connection. #! /usr/bin/python import sys import sqlalchemy as sa def handle_checkout_event(dbapi_con, con_record, con_proxy): try: with dbapi_con.cursor() as cur: cur.execute("SELECT 1") cur.fetchone() except Exception, e: raise sa.exc.DisconnectionError() def main(): uri = sys.argv[1] #engine = sa.create_engine(uri, poolclass=sa.pool.AssertionPool) engine = sa.create_engine(uri, pool_size=1, max_overflow=0, pool_timeout=0, pool_recycle=3600) sa.event.listen(engine, 'checkout', handle_checkout_event) with engine.begin() as conn: pass print "Now stop PG." raw_input('-=>') # should get an operational error try: with engine.begin() as conn: pass raw_input('-=>') except sa.exc.OperationalError, e: print >>sys.stderr, "Got an (expected) error: ", e raw_input('-=!') # *should* get the same thing. try: with engine.begin() as conn: pass raw_input('-=>') except sa.exc.OperationalError, e: print >>sys.stderr, "Got an (expected) error: ", e print "We never get here." raw_input('-=!') if __name__ == '__main__': main() -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.