Psycopg2: What is "best practice" way to handle lost of connection/reconnection

Created on 28 Mar 2016  ·  2Comments  ·  Source: psycopg/psycopg2

I've seen https://github.com/psycopg/psycopg2/issues/369 and I was wondering if the status has changed, and if not what is the currently recommanded way to handle that ?

This question on stackoverflow has several answers , but they all start to date a bit:
http://stackoverflow.com/questions/1281875/making-sure-that-psycopg2-database-connection-alive

So I was wondering if it could be included in the documentation how to handle this situation , especially as I think it's quite common in web services.

question

All 2 comments

You may check conn.get_transaction_status() to detect a broken connection after an operation. For an example check the pool code

There is no way to know if there is an error _before_ performing an operation, if not... performing an operation. For this reason it is not implemented in the driver. If your app can afford performing an extra roundtrip per request you may query a select 1 on the connection before using it.

ok thanks, for the "select 1" , yes, actually I was wondering if there was some mechanism in the postgresql's protocol like the 'ping' in mysql's, but it seems not.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jasonrubenstein picture jasonrubenstein  ·  5Comments

vitorpontual picture vitorpontual  ·  5Comments

anarazel picture anarazel  ·  3Comments

yamagoya picture yamagoya  ·  5Comments

ProgVal picture ProgVal  ·  3Comments