mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-01-31 09:24:07 +03:00
Always raise OperationalError when connection was closed externally.
From the DB-API (https://www.python.org/dev/peps/pep-0249/): OperationalError Exception raised for errors that are related to the database's operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, [...] Additionally, psycopg2 was inconsistent, at least in the async case: depending on how the "connection closed" error was reported from the kernel to libpq, it would sometimes raise OperationalError and sometimes DatabaseError. Now it always raises OperationalError.
This commit is contained in:
parent
b203a7c775
commit
12317557da
|
@ -179,8 +179,10 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres)
|
||||||
|
|
||||||
/* if the connection has somehow been broken, we mark the connection
|
/* if the connection has somehow been broken, we mark the connection
|
||||||
object as closed but requiring cleanup */
|
object as closed but requiring cleanup */
|
||||||
if (conn->pgconn != NULL && PQstatus(conn->pgconn) == CONNECTION_BAD)
|
if (conn->pgconn != NULL && PQstatus(conn->pgconn) == CONNECTION_BAD) {
|
||||||
conn->closed = 2;
|
conn->closed = 2;
|
||||||
|
exc = OperationalError;
|
||||||
|
}
|
||||||
|
|
||||||
if (pgres == NULL && curs != NULL)
|
if (pgres == NULL && curs != NULL)
|
||||||
pgres = &curs->pgres;
|
pgres = &curs->pgres;
|
||||||
|
@ -214,9 +216,9 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres)
|
||||||
if (code != NULL) {
|
if (code != NULL) {
|
||||||
exc = exception_from_sqlstate(code);
|
exc = exception_from_sqlstate(code);
|
||||||
}
|
}
|
||||||
else {
|
else if (exc == NULL) {
|
||||||
/* Fallback if there is no exception code (reported happening e.g.
|
/* Fallback if there is no exception code (unless we already
|
||||||
* when the connection is closed). */
|
determined that the connection was closed). */
|
||||||
exc = DatabaseError;
|
exc = DatabaseError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -575,7 +575,7 @@ class CursorTests(ConnectingTestCase):
|
||||||
cur.execute('select pg_terminate_backend(%s)', (pid1,))
|
cur.execute('select pg_terminate_backend(%s)', (pid1,))
|
||||||
|
|
||||||
time.sleep(0.001)
|
time.sleep(0.001)
|
||||||
with self.assertRaises(psycopg2.DatabaseError):
|
with self.assertRaises(psycopg2.OperationalError):
|
||||||
with victim_conn.cursor() as cur:
|
with victim_conn.cursor() as cur:
|
||||||
cur.execute('select 1')
|
cur.execute('select 1')
|
||||||
wait_func(victim_conn)
|
wait_func(victim_conn)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user