I have a daemon that uses sqlalchemy to interact with a MySQL database. Because communication is rare, connections are subject to temporary disconnection. I tried to fix the problem by setting various flags when creating the database engine, for example. pool_recycle=3600
, but nothing helps.
To help me debug the problem, I set the timeout of my local mysql server to 10 seconds and tried the following program.
import time import sqlalchemy engine = sqlalchemy.engine.create_engine("mysql://localhost") while True: connection = engine.connect() result = connection.execute("SELECT 1") print result.fetchone() connection.close() time.sleep(15)
Surprisingly, I keep getting exceptions, such as:
sqlalchemy.exc.OperationalError: (OperationalError) (2006, 'MySQL server has gone away')
However, if I delete the connection.close()
call, the problem will disappear. What's going on here? Why is sqlalchemy trying to establish a new connection every time I call connect()
?
I am using Python 2.7.3 with sqlalchemy 0.9.8 and MySQL 5.5.40.
source share