I have seen this question multiple times on here with answers that did not help me.

From my understanding, Flask-SQLAlchemy connects to the database at the beginning of the Flask request and tears down this session when done. But what if this request takes a long time?

For example, my app accepts a POST request that starts a long-running Celery task that needs to write to the database. The MySQL server "goes away" after some time, making the write back to the database at the end of the task not work.

I read online to set `options["pool_pre_ping"] = True` to mitigate such a problem (using the subclassing SQLAlchemy workaround found [here](https://github.com/pallets/flask-sqlalchemy/issues/589)), but that makes no difference. The same error occurs and it almost seems that no settings actually seem to change.

I think the solution is to rethink how the task writes to the database. Currently, I am using the same `session` from the request for the task. Any ideas if this is wrong?