Home > Mobile >  Database connection broken even with with SQLAlchemy pre-ping
Database connection broken even with with SQLAlchemy pre-ping

Time:01-12

Since I moved my database from a mssql docker container to an azure database as a service my web application( flask) is being disconnected after several minutes.

It is simply solved by a refresh of the page but still, there is an issue here.

The error raised by SQLAlchemy is an Operational Error (https://docs.sqlalchemy.org/en/14/errors.html#error-e3q8)

I tried to implement the pool_pre_ping=True option in the create_engine function the error is not solved.

I think the disconnect is initiated by the database but I do not know if there is an option to change that. (database is SQL Server 12.0)

CodePudding user response:

I had same issue before. I solved it by pool_recycle option of create_engine function.

The database has default connection maintain time itself. When the connection is not used during certain period (DB setting), then DB close that connection.

So, You need set pool_recycle time lower then db connection maintain time.

Example,

create_engine("...", pool_recycle=3600, pool_pre_ping=True)

Check document for more infomation

CodePudding user response:

As I am using Flask-SQLAlchemy there are configuration parameters to recycle the pool.

I solved my problem by removing the create_engine function.

I simply add SQLALCHEMY_POOL_RECYCLE = 300 to my configuration file.

  •  Tags:  
  • Related