Forums

MySQL Database Hanging

Hello fellow PythonAnywhere support members, I am currently building a website using your platform combining Next.js + Flask. To store data from the website I use the onboard MySQL server provided by you guys, but it's been causing me a little bit of trouble over the past couple of days.

It hangs up on me all the sudden after not making requests for an estimated time period of atleast five minutes... When you look at it from a resource POV, that's great and all, but when a user would want to do an action, the server wouldn't respond.

I also have a piece of code to catch a situation like this and the code does infact return maintenacne, when I refresh my app again, it's all good.

@app.before_request
def is_database_online():
    if request.method == "POST" or request.method == "GET":
        try:
            db.create_all()
            session['MAINTENANCE'] = "NO"
            if request.path == "/":
                return {"MAINTENANCE":False}
        except:
            traceback.print_exc()
            if "MAINTENANCE" in session.keys() and session['MAINTENANCE'] == "TRUE":
                return {"MAINTENANCE":"ON-GOING"}
            else:
                session["MAINTENANCE"] = "TRUE"
                return {"MAINTENANCE":True}

Thanks in advance for any help or clarification, Niv.

We time out connections after 5 minutes. make sure that you have code to handle re-connecting when the connection is lost.

Also, from that code, it looks like you're trying to recreate all of your tables before every request. That's not likely to be good for the performance of your database and your site.

Yes I will change that bit, it's just my current comfortable way of checking if it's alive, would you be able to offer me a different solution I can implement? I am using flask-sqlalchemy.

I'm not sure what is the problem that you want to find a different solution for.