python - SQLAlchemy error MySQL server has gone away -
error operationalerror: (operationalerror) (2006, 'mysql server has gone away')
i'm received error when coded project on flask, cant understand why error.
i have code (yeah, if code small , executing fast, no errors) \
db_engine = create_engine('mysql://root@127.0.0.1/mind?charset=utf8', pool_size=10, pool_recycle=7200) base.metadata.create_all(db_engine) session = sessionmaker(bind=db_engine, autoflush=true) session = scoped_session(session) session = session() # there many classes , functions session.close()
and code returns me error 'mysql server has gone away'
, return after time, when use pauses in script.
mysql use openserver.ru (it's web server such wamp).
thanks..
sqlalchemy has great write-up on how can use pinging pessimistic connection's freshness:
http://docs.sqlalchemy.org/en/latest/core/pooling.html#disconnect-handling-pessimistic
from there,
from sqlalchemy import exc sqlalchemy import event 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 whole pool # instead of invalidating 1 @ time # connection_proxy._pool.dispose() # raise disconnectionerror - pool try # connecting again 3 times before raising. raise exc.disconnectionerror() cursor.close()
and test make sure above works:
from sqlalchemy import create_engine e = create_engine("mysql://scott:tiger@localhost/test", echo_pool=true) c1 = e.connect() c2 = e.connect() c3 = e.connect() c1.close() c2.close() c3.close() # pool size three. print "restart server" raw_input() in xrange(10): c = e.connect() print c.execute("select 1").fetchall() c.close()
Comments
Post a Comment