Forums

The client was disconnected by the server because of inactivity.

Hello, when i'm trying to load a page with information from my SQLDatabase, i often have this type of error : "The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior." I have to refresh the page once to fix it temporarily . I tried to add this line to my app.py but it didn't help or maybe i missed something : app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_recycle' : 280}, Thanks in advance for your advices.

That looks like the correct code to set the connection management timeout parameters, so it should fix the issue. Could you post all of the code that you're using to set up the database connection? You can replace the password and any other secret stuff (like the app's secret key) in the posted code with something like [REDACTED].

it goes like that. Does the order have to do something with it ?

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://XXX:XXX@XXX.mysql.pythonanywhere-services.com/XXX$XXX'
app.config['SECRET_KEY'] = 'XXX'
db = SQLAlchemy(app)
migrate = Migrate(app, db)
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_recycle' : 280}

Yes, I think you've spotted the problem -- the SQLAlchemy library will read in those settings at the time that it's created. So if you put your last line, where you do the pool_recycle stuff, above that line -- perhaps immediately after the line where you set the SQLALCHEMY_DATABASE_URI -- then that should fix the problem.

Hello, sorry I'm reviving this thread. I'm trying to modify the database from a discord bot. Based on advices from here, I created another file db.py which I imported on my app.py and bot.py When I'm trying to connect to the database from the app.py aka the website, it works fine, however the bot can access to the database only a short time after its initialization I'm getting the same message as the title says. My discord bot is handled by an always-on task so it might be the wrong way to do it. Feel free to look at my code If you need ^^

How are you managing the connections? Are you initializing the db connection only at the import time?

I think the connection to the database is established when these models are imported and utilized for queries or other database operations. Since I've add the db initialization in a seperate file from the webapp, for now there is a connection made in the webapp and another one for the bot.py in the db.py file. I've done this to avoid a circular import.

Just to make sure -- you are initializing the db object in your db.py file, so that it has all of the same parameters applied as it does in the website's code; is that right?

Yes

OK, from looking at your code and the error in question, it looks like it's happening in a line doing this:

db.session.rollback()

That is happening in a function that hasn't done anything with the database yet; what happens if you remove it?

Oh, maybe I misunderstood the usage of this line but I wanted to force reset the current session to avoid the timout. I'm so lost cause before adding this line on every database usage, the bot wasn't functionnal on every function short after the bot's init (aclient.py event and bot.py event) With this line, the on_message(message) event in bot.py was functionnal but only half of the time for my members.

I tried to remove every rollback() line but it does the same.

I think that perhaps the Flask-SQLAlchemy plugin is dependent on the Flask context when it tries to create MySQL connections. My experience with this kind of thing is more Django than Flask, but perhaps what you might need is a custom command? They appear to be similar to Django's custom management commands, which are the way I would recommend doing stuff that requires access to the ORM from code running outside the context of a website.