Operational Error: An Existing connection was forcibly closed by the remote host. (10054)

771 Views Asked by At

Am getting this Operational Error, periodically probably when the application is not active or idle for long hours. On refreshing the page it will vanish. Am using mssql pyodbc connection string ( "mssql+pyodbc:///?odbc_connect= ...") in Formhandlers and DbAuth of gramex

How Can I keep the connection alive in gramex?

Screenshot of error

1

There are 1 best solutions below

0
On

Add pool_pre_ping and pool_recycle parameters.

  • pool_pre_ping will normally emit SQL equivalent to “SELECT 1” each time a connection is checked out from the pool; if an error is raised that is detected as a “disconnect” situation, the connection will be immediately recycled. Read more
  • pool_recycle prevents the pool from using a particular connection that has passed a certain age. Read more

eg: engine = create_engine(connection_string, encoding='utf-8', pool_pre_ping=True, pool_recycle=3600)

Alternatively, you can add these parameters for FormHandler in gramex.yaml. This is required only for the first FormHandler with the connection string.

kwargs:
 url: ...
 table: ...
 pool_pre_ping: True
 pool_recycle: 60