I'm using Flask-SQLAlchemy 1.0, Flask 0.10, SQLAlchemy 0.8.2, and Python 2.7.5. I'm connecting to MySQL 5.6 with Oracle's MySQL Connector/Python 1.0.12.
When I restart my web server (either Apache2 or Flask's built-in), I receive the exception
after MySQL's
expires (default 8 hours).
I've found people with <a href="http://mofanim.wordpress.com/2013/01/02/sqlalchemy-mysql-has-gone-away/" rel="nofollow">similar problems</a> and explicitly set
, even though that's <a href="https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/flask_sqlalchemy/__init__.py#L839" rel="nofollow">Flask-SQLAlchemy's default</a>. When I put a breakpoint <a href="https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/flask_sqlalchemy/__init__.py#L826" rel="nofollow">here</a>, I see that the teardown function is successfully calling
after each request. Any ideas?
<strong>Update 7/21/2014:</strong>
Since this question continues to receive attention, I must add that I <em>did</em> try some of the proposals. Two of my attempts looked like the following:
<strong>First:</strong>
This allowed me to wrap my commit calls like so:
I am 99% certain that I did not miss any
calls with this method and I still had problems.
<strong>Second:</strong>
To further ensure I wasn't missing any commit calls, I made a Flask wrapper that enabled code such as (if I remember correctly):
Unfortunately, neither method worked. I also recall trying to issue SELECT(1) calls in an attempt to re-establish the connection, but I don't have that code anymore.
To me, the bottom line is MySQL/SQL Alchemy has issues. When I migrated to Postgres, I didn't have to worry about my commits. Everything just worked.
When I restart my web server (either Apache2 or Flask's built-in), I receive the exception
Code:
OperationalError: MySQL Connection not available
Code:
wait_timeout
I've found people with <a href="http://mofanim.wordpress.com/2013/01/02/sqlalchemy-mysql-has-gone-away/" rel="nofollow">similar problems</a> and explicitly set
Code:
SQLALCHEMY_POOL_RECYCLE = 7200
Code:
session.remove()
<strong>Update 7/21/2014:</strong>
Since this question continues to receive attention, I must add that I <em>did</em> try some of the proposals. Two of my attempts looked like the following:
<strong>First:</strong>
Code:
@contextmanager
def safe_commit():
try:
yield
db.session.commit()
except:
db.session.rollback()
raise
This allowed me to wrap my commit calls like so:
Code:
with safe_commit():
model = Model(prop=value)
db.session.add(model)
I am 99% certain that I did not miss any
Code:
db.session.commit
<strong>Second:</strong>
Code:
def managed_session():
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
try:
response = f(*args, **kwargs)
db.session.commit()
return response
except:
db.session.rollback()
raise
finally:
db.session.close()
return decorated_function
return decorator
To further ensure I wasn't missing any commit calls, I made a Flask wrapper that enabled code such as (if I remember correctly):
Code:
@managed_session()
def hello(self):
model = Model(prop=value)
db.session.add(model)
return render_template(...
Unfortunately, neither method worked. I also recall trying to issue SELECT(1) calls in an attempt to re-establish the connection, but I don't have that code anymore.
To me, the bottom line is MySQL/SQL Alchemy has issues. When I migrated to Postgres, I didn't have to worry about my commits. Everything just worked.