diff options
| author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2010-04-05 00:52:50 +0100 |
|---|---|---|
| committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2010-04-21 15:21:32 +0100 |
| commit | 7a06c0455b914ba3fc4a8a819aba2e95d78bda74 (patch) | |
| tree | 933651bdcc24d1167aa291f39fbe062aa5999156 /psycopg/green.c | |
| parent | a54932ee9c086f658881b4d1141646d82fdc98db (diff) | |
| download | psycopg2-7a06c0455b914ba3fc4a8a819aba2e95d78bda74.tar.gz | |
Try to restore the connection state after a wait callback error.
Diffstat (limited to 'psycopg/green.c')
| -rw-r--r-- | psycopg/green.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/psycopg/green.c b/psycopg/green.c index fc9605d..df1c02e 100644 --- a/psycopg/green.c +++ b/psycopg/green.c @@ -33,6 +33,7 @@ HIDDEN PyObject *wait_callback = NULL; PyObject *have_wait_callback(void); +void psyco_clear_result_blocking(connectionObject *conn); /* Register a callback function to block waiting for data. * @@ -163,6 +164,7 @@ psyco_exec_green(connectionObject *conn, const char *command) pyrv = PyObject_CallFunctionObjArgs(cb, conn, NULL); if (!pyrv) { Dprintf("psyco_exec_green: error in callback sending query"); + psyco_clear_result_blocking(conn); goto clear; } Py_DECREF(pyrv); @@ -173,6 +175,7 @@ psyco_exec_green(connectionObject *conn, const char *command) pyrv = PyObject_CallFunctionObjArgs(cb, conn, NULL); if (!pyrv) { Dprintf("psyco_exec_green: error in callback reading result"); + psyco_clear_result_blocking(conn); goto clear; } Py_DECREF(pyrv); @@ -197,3 +200,23 @@ end: return result; } + +/* Discard the result of the currenly executed query, blocking. + * + * This function doesn't honour the wait callback: it can be used in case of + * emergency if the callback fails in order to put the connection back into a + * consistent state. + * + * If any command was issued before clearing the result, libpq would fail with + * the error "another command is already in progress". + */ +void +psyco_clear_result_blocking(connectionObject *conn) +{ + PGresult *res; + + Dprintf("psyco_clear_result_blocking"); + while (NULL != (res = PQgetResult(conn->pgconn))) { + PQclear(res); + } +} |
