Skip to content

The transaction block doesn't check the connection before attempting to roll back #165

@dvarrazzo

Description

@dvarrazzo

The connection block is more well behaved:

>>> with psycopg.connect() as cnn:
...     cnn.execute("select 1")
...     cnn.close()
...     1/0
... 
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
ZeroDivisionError: division by zero

The transaction block doesn't check the connection before trying a rollback:

>>> cnn = psycopg.connect()
>>> with cnn.transaction():
...     cnn.execute("select 1")
...     cnn.close()
...     1/0
... 
Traceback (most recent call last):
  File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 744, in transaction
    yield tx
  File "<stdin>", line 4, in <module>
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/usr/lib/python3.8/contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 744, in transaction
    yield tx
  File "/home/piro/dev/psycopg3/psycopg/psycopg/transaction.py", line 205, in __exit__
    return self._conn.wait(self._exit_gen(exc_type, exc_val, exc_tb))
  File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 767, in wait
    return waiting.wait(gen, self.pgconn.socket, timeout=timeout)
  File "psycopg_c/pq/pgconn.pyx", line 186, in psycopg_c.pq.PGconn.socket.__get__
  File "psycopg_c/pq/pgconn.pyx", line 632, in psycopg_c.pq._call_int
  File "psycopg_c/pq/pgconn.pyx", line 614, in psycopg_c.pq._ensure_pgconn
psycopg.OperationalError: the connection is closed

leaving the transaction block should behave like for the connection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions