I'm using Flask SQLAlchemy to connect to my mysql db, but it has defualt wait_timeout 120 seconds, so after I query my users and web isn't used for a while I get an error
(2013, 'Lost connection to MySQL server during query')
Important piece of my db.py
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://urltomyserver'
app.config['SQLALCHEMY_POOL_RECYCLE'] = 10
app.config['SQLALCHEMY_POOL_TIMEOUT'] = 120
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
if __name__ == 'createdb':
db.reflect()
db.drop_all()
db = SQLAlchemy(app)
And piece of my views.py
@core.route('/')
def index():
userzy = sftpuser.query.all()
return render_template('index.html', userzy=userzy)
#I'D LIKE TO CLOSE MY CONNECTION HERE
I've tried these under after I return template in def index
db.session.close()
db.close()
db.dispose()
db.session.close()
db.engine.dispose()
db.session.commit()
And this
@app.teardown_appcontext
def teardown_db(error):
db.session.close()
db.engine.dispose()
But it didnt help me that much, anyone knows the solution why do am I keep getting error even if I set the pool?