persistent db connections/resiliency

Started by Robert Boydover 25 years ago2 messagesgeneral
Jump to latest
#1Robert Boyd
rboyd@ivmg.net

I'm tasked with handling database connection failures
elegantly--reconnecting after failure.

I'm wondering how other people have approached this problem.

Should I get connection status before every query, and if bad, attempt
reconnect?

Any help is appreciated.

- Rob

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Boyd (#1)
Re: persistent db connections/resiliency

Robert Boyd <rboyd@ivmg.net> writes:

Should I get connection status before every query, and if bad, attempt
reconnect?

Waste of time, assuming you are thinking of PQstatus(); it just returns
a field from the PGconn struct showing the last known state.

I'd suggest that *upon error* you check to see if the error indicates
a dead connection, and attempt reconnect if so. No need to expend
cycles in the main line of your app for this.

If you really want an end-to-end connectivity check, you can issue
an empty query string with PQexec; but I'd not recommend doing that
for every single query...

regards, tom lane