CheckConnection * * Verify that we still have a good connection to the backend, and if not, * see if it can be restored. * * Returns true if either the connection was still there, or it could be * restored successfully; false otherwise. If, however, there was no * connection and the session is non-interactive, this will exit the program * with a code of EXIT_BADCONN. */
| 292 | * with a code of EXIT_BADCONN. |
| 293 | */ |
| 294 | static bool |
| 295 | CheckConnection(void) |
| 296 | { |
| 297 | bool OK; |
| 298 | |
| 299 | OK = ConnectionUp(); |
| 300 | if (!OK) |
| 301 | { |
| 302 | if (!pset.cur_cmd_interactive) |
| 303 | { |
| 304 | pg_log_fatal("connection to server was lost"); |
| 305 | exit(EXIT_BADCONN); |
| 306 | } |
| 307 | |
| 308 | fprintf(stderr, _("The connection to the server was lost. Attempting reset: ")); |
| 309 | PQreset(pset.db); |
| 310 | OK = ConnectionUp(); |
| 311 | if (!OK) |
| 312 | { |
| 313 | fprintf(stderr, _("Failed.\n")); |
| 314 | |
| 315 | /* |
| 316 | * Transition to having no connection; but stash away the failed |
| 317 | * connection so that we can still refer to its parameters in a |
| 318 | * later \connect attempt. Keep the state cleanup here in sync |
| 319 | * with do_connect(). |
| 320 | */ |
| 321 | if (pset.dead_conn) |
| 322 | PQfinish(pset.dead_conn); |
| 323 | pset.dead_conn = pset.db; |
| 324 | pset.db = NULL; |
| 325 | ResetCancelConn(); |
| 326 | UnsyncVariables(); |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | fprintf(stderr, _("Succeeded.\n")); |
| 331 | |
| 332 | /* |
| 333 | * Re-sync, just in case anything changed. Keep this in sync with |
| 334 | * do_connect(). |
| 335 | */ |
| 336 | SyncVariables(); |
| 337 | connection_warnings(false); /* Must be after SyncVariables */ |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return OK; |
| 342 | } |
| 343 | |
| 344 | |
| 345 |
no test coverage detected