.. autoattribute:: savepoint_name
.. autoattribute:: connection
+ .. autoattribute:: status
+
+ The current status of the transaction. Returns a `Transaction.Status`
+ enum value indicating whether the transaction is active, committed,
+ rolled back, or failed.
+
+ Possible values:
+
+ - `!NOT_STARTED`: Transaction created but not yet entered
+ - `!ACTIVE`: Transaction is currently active
+ - `!COMMITTED`: Transaction successfully committed
+ - `!ROLLED_BACK_EXPLICITLY`: Transaction explicitly rolled back
+ - `!ROLLED_BACK_WITH_ERROR`: Transaction rolled back due to an error
+ - `!FAILED`: Transaction failed due to connection failure
+
+ See :ref:`transaction-status` for more details and examples.
.. autoclass:: AsyncTransaction()
.. autoattribute:: connection
+ .. autoattribute:: status
+
+ The current status of the transaction. Returns a `AsyncTransaction.Status`
+ enum value. See `Transaction.status` for details.
.. autoexception:: Rollback
.. index:: Transactions management
.. index:: InFailedSqlTransaction
.. index:: idle in transaction
+.. index:: Transaction status
.. _transactions:
# and the program would continue from here with no exception.
+.. _transaction-status:
+
+Transaction status
+^^^^^^^^^^^^^^^^^^
+
+Each `Transaction` object exposes a `~Transaction.status` property that allows
+you to inspect the current state of the transaction. This can be useful for
+debugging, logging, or implementing custom transaction management logic.
+
+The `!status` property returns a `Transaction.Status` enum value that can be
+one of the following:
+
+- `!NOT_STARTED`: The transaction has been created but not yet entered (before
+ the `!__enter__()` method is called).
+- `!ACTIVE`: The transaction is currently active (inside the `!with` block).
+- `!COMMITTED`: The transaction exited successfully and changes were committed.
+- `!ROLLED_BACK_EXPLICITLY`: The transaction was explicitly rolled back, either
+ by raising a `Rollback` exception or by using `!force_rollback=True`.
+- `!ROLLED_BACK_WITH_ERROR`: The transaction was rolled back due to an
+ exception raised within the transaction block.
+- `!FAILED`: The transaction failed due to a connection failure (e.g., the
+ connection was closed or became unavailable during the transaction).
+
+Here's an example of how to use the status property:
+
+.. code:: python
+
+ from psycopg import Rollback
+
+ # Successful transaction
+ with conn.transaction() as tx:
+ assert tx.status == tx.Status.ACTIVE
+ conn.execute("INSERT INTO data VALUES (%s)", ("Hello",))
+ assert tx.status == tx.Status.COMMITTED
+
+ # Transaction rolled back with an error
+ try:
+ with conn.transaction() as tx:
+ conn.execute("INSERT INTO data VALUES (%s)", ("World",))
+ 1 / 0 # Causes an exception
+ except ZeroDivisionError:
+ pass
+ assert tx.status == tx.Status.ROLLED_BACK_WITH_ERROR
+
+ # Explicit rollback
+ with conn.transaction() as tx:
+ conn.execute("INSERT INTO data VALUES (%s)", ("Test",))
+ raise Rollback()
+ assert tx.status == tx.Status.ROLLED_BACK_EXPLICITLY
+
+ # Force rollback
+ with conn.transaction(force_rollback=True) as tx:
+ conn.execute("INSERT INTO data VALUES (%s)", ("Temp",))
+ assert tx.status == tx.Status.ROLLED_BACK_EXPLICITLY
+
+The `!status` property remains accessible after the transaction context has
+exited, allowing you to check the final outcome of the transaction even after
+the `!with` block completes. This is particularly useful when you need to log
+transaction outcomes or implement retry logic based on the transaction status.
+
+
.. _transaction-characteristics:
Transaction characteristics