天天看点

SqlAlchemy: (OperationalError) (2006, ‘MySQL server has gone away’)

What is this problem about:

This is actually MySQL error message. It means that there are no MySQLconnections anymore.By MySQL default, idling database connections will be closed after 8 hours. Not having MySQL connections means that no SQL operations can be performed. SqlAlchemy is simple reporting the error.

When using SQLAlchemy, how to prevent such problems?

See sample code below:

engine = create_engine(db_path, pool_size = 100, pool_recycle=7200)

 Take a look at the pool_recycle variable. That will configure SqlAlchemy engine to reconnect every x number of seconds. This easy solution makes sure the existence of MySQL connection.

http://www.sqlalchemy.org/docs/core/engines.html?highlight=create_engine#sqlalchemy.create_engine

References:

  • SqlAlchemy Trac – FAQ
  • Mail Archive – MySQL server has gone away 
  • GoogleGroup – Has Gone Away