Forums

Database disconnects occasionally - Flask/sqlalchemy/mysql

My web app losts connection with the MySQL database occasionally, usually after 5-10 mins. I read related threads and understand that it shoud be pool_recycle time so I set it to be 20. But still, the problem still exists. Therefore, I enclosed part of my codes below and hope someone could help. Thanks in advance!!!!

from flask import Flask, render_template, request from sqlalchemy
import create_engine from sqlalchemy.orm 
import sessionmaker from database_setup import Base, Bus, Station, Route, RouteStation

app = Flask(__name__)

engine = create_engine('mysql+mysqlconnector://nrator:something@nrator.mysql.pythonanywhere-services.com/nrator$bus?charset=utf8', pool_recycle=20)

Base.metadata.bind = engine

DBSession = sessionmaker(bind=engine) 
session = DBSession()

app = Flask(__name__)
...