exc_tb: Optional[TracebackType],
) -> None:
if exc_type:
- self.rollback()
+ # try to rollback, but if there are problems (connection in a bad
+ # state) just warn without clobbering the exception bubbling up.
+ try:
+ self.rollback()
+ except Exception as exc2:
+ warnings.warn(
+ f"error rolling back the transaction on {self}: {exc2}",
+ RuntimeWarning,
+ )
else:
self.commit()
exc_tb: Optional[TracebackType],
) -> None:
if exc_type:
- await self.rollback()
+ # try to rollback, but if there are problems (connection in a bad
+ # state) just warn without clobbering the exception bubbling up.
+ try:
+ await self.rollback()
+ except Exception as exc2:
+ warnings.warn(
+ f"error rolling back the transaction on {self}: {exc2}",
+ RuntimeWarning,
+ )
else:
await self.commit()
cur.execute("select * from textctx")
+def test_context_rollback_no_clobber(conn, dsn, recwarn):
+ with pytest.raises(ZeroDivisionError):
+ with psycopg3.connect(dsn) as conn2:
+ conn2.execute("select 1")
+ conn.execute(
+ "select pg_terminate_backend(%s::int)",
+ [conn2.pgconn.backend_pid],
+ )
+ 1 / 0
+
+ assert "rolling back" in str(recwarn.pop(RuntimeWarning).message)
+
+
def test_weakref(dsn):
conn = psycopg3.connect(dsn)
w = weakref.ref(conn)
await cur.execute("select * from textctx")
+async def test_context_rollback_no_clobber(conn, dsn, recwarn):
+ with pytest.raises(ZeroDivisionError):
+ async with await psycopg3.AsyncConnection.connect(dsn) as conn2:
+ await conn2.execute("select 1")
+ conn.execute(
+ "select pg_terminate_backend(%s::int)",
+ [conn2.pgconn.backend_pid],
+ )
+ 1 / 0
+
+ assert "rolling back" in str(recwarn.pop(RuntimeWarning).message)
+
+
async def test_weakref(dsn):
conn = await psycopg3.AsyncConnection.connect(dsn)
w = weakref.ref(conn)