Problem: Despite configuring pool_pre_ping, pool_recycle, and other SQLAlchemy engine options to manage MySQL connections, I'm facing frequent disconnections due to inactivity somewhere near 5 minutes (I´ve read in this forum that 5 is the default setting for inactivity limit on PythonAnywhere and that MySQL configuration should override that and it should be fine but it does not seem to work, probably I am doing something wrong).
Details:
SQLAlchemy settings:
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+mysqlconnector://user:password@host/database"
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
"pool_pre_ping": True,
"pool_size": 20,
"max_overflow": 10,
"pool_timeout": 10,
"pool_recycle": 280
}
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'], **app.config["SQLALCHEMY_ENGINE_OPTIONS"])
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
db.init_app(app)
Steps Taken:
Verified MySQL wait_timeout and interactive_timeout settings. Ensured no database access outside Flask view functions because I also followed this. Also saw this and tried without the pre_ping element but it still didn´t work.
I am not very experienced in web development much less in server or db configuration, but I think that the problem here lies in MySQL configuration not overriding PythonAnywhere default configuration and I am not being able to make it work on my own.
Greetings from Argentina. Thank you!