开发者

sqlalchemy fails to connect to db after server is idle for a few hours

I have a fairly standard setup where the application server is separated by a firewall from the database server. The application us using sqlalchemy and the db is postgres.

The system works fine for a few hours, but once it remains idle for a long period it appears that the communication fails between the server and the db. The error log is:

WARNI [sqlalchemy.pool.QueuePool.0x...7310] [worker 26] Error closing cursor: cursor already closed

I set up the c开发者_如何学编程onnection pool with the 'pool_recylce' setting set to 30mins, so that there are no connections lingering for more than 30 mins. The problem persists.

any ideas?


You probably have a stateful firewall between a client and a server, which is forgetting about connections that are idle for too long.

You need either to:

  • configure your firewall to remember connections longer — it may not be possible though;
  • configure keepalive on the server, for example like this:
    tcp_keepalives_idle=600
    tcp_keepalives_interval=30
    tcp_keepalives_count=60
    
    Which means that if connection is idle for 10 minutes try to send keepalive probes every 30 seconds until response, close connection if 60 probes are ignored (connection is broken for 30 minutes);
  • configure keepalive in client using `keepalives_idle`, `keepalives_interval` and `keepalives_count` connection parameters; I don't know if it is possible in SQLAlchemy;

Disable pool_recycle as IMHO it works only if you close a connection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜