Files
tryton/trytond/doc/ref/transaction.rst
T

176 lines
5.4 KiB
ReStructuredText
Raw Normal View History

2010-08-19 22:01:12 +02:00
.. _ref-transaction:
.. module:: trytond.transaction
Transaction
===========
2022-12-26 15:05:56 +01:00
.. exception:: TransactionError
The base class for transaction error that need to retry the transaction.
.. method:: TransactionError.fix(extras)
Update the extras argument of :meth:`~Transaction.start` to restart the
transaction without the error.
2010-08-19 22:01:12 +02:00
.. class:: Transaction
2022-03-27 16:06:09 +02:00
Represents a Tryton transaction that contains thread-local parameters of a
database connection.
The Transaction instances are `context manager`_ that commits or
rollbacks the database transaction.
In the event of an exception the transaction is rolled back, otherwise it is
commited.
2010-08-19 22:01:12 +02:00
.. attribute:: Transaction.database
2022-03-27 16:06:09 +02:00
The database.
.. attribute:: Transaction.readonly
.. attribute:: Transaction.connection
2022-03-27 16:06:09 +02:00
The database connection as defined by the `PEP-0249`_.
2010-08-19 22:01:12 +02:00
.. attribute:: Transaction.user
2022-03-27 16:06:09 +02:00
The id of the user.
2010-08-19 22:01:12 +02:00
.. attribute:: Transaction.context
.. attribute:: Transaction.create_records
.. attribute:: Transaction.delete_records
2020-03-28 17:13:12 +01:00
.. attribute:: Transaction.trigger_records
2022-04-08 09:50:47 +02:00
.. attribute:: Transaction.check_warnings
The set of warnings already checked.
2010-08-19 22:01:12 +02:00
.. attribute:: Transaction.timestamp
.. attribute:: Transaction.started_at
2022-03-27 16:06:09 +02:00
The monotonic timestamp when the transaction started.
2010-08-19 22:01:12 +02:00
.. attribute:: Transaction.language
2022-03-27 16:06:09 +02:00
The language code defines in the context.
2010-08-19 22:01:12 +02:00
2012-09-24 22:16:53 +02:00
.. attribute:: Transaction.counter
2022-03-27 16:06:09 +02:00
Count the number of modification made in this transaction.
2012-09-24 22:16:53 +02:00
.. attribute:: Transaction.check_access
If the access must be enforced.
.. attribute:: Transaction.active_records
2023-07-05 12:57:15 +02:00
If the active test is enabled for :class:`~trytond.model.DeactivableMixin`.
.. staticmethod:: Transaction.monotonic_time
2023-02-04 21:02:37 +01:00
Return a monotonic time used to populate :attr:`~Transaction.started_at`.
2022-12-26 15:05:56 +01:00
.. method:: Transaction.start(database_name, user[, readonly[, context[, close[, autocommit, \**extras]]]])
2010-08-19 22:01:12 +02:00
2022-03-27 16:06:09 +02:00
Start a new transaction and return a `context manager`_.
The non-readonly transaction will be committed when exiting the ``with``
statement without exception.
The other cases will be rollbacked.
2010-08-19 22:01:12 +02:00
.. method:: Transaction.stop([commit])
2022-03-27 16:06:09 +02:00
Stop the transaction.
If commit is ``True``, the transaction will be committed otherwise it will
be rollbacked.
The `context manager`_ returned by :meth:`~Transaction.start` should be used
instead of calling this method.
2010-08-19 22:01:12 +02:00
.. method:: Transaction.set_context(context, \**kwargs)
2022-03-27 16:06:09 +02:00
Update the transaction context and return a `context manager`_.
The context is restored when exiting the ``with`` statement.
2010-08-19 22:01:12 +02:00
.. method:: Transaction.reset_context()
2022-03-27 16:06:09 +02:00
Clear the transaction context and return a `context manager`_.
The context is restored when exiting the ``with`` statement.
2010-08-19 22:01:12 +02:00
.. method:: Transaction.set_user(user[, set_context])
2022-03-27 16:06:09 +02:00
Modify the user of the transaction and return a `context manager`_.
``set_context`` will put the previous user id in the context to simulate the
record rules.
The user will be restored when exiting the ``with`` statement.
2010-08-19 22:01:12 +02:00
2022-12-26 15:05:56 +01:00
.. method:: Transaction.lock_table(table)
Raise a :exc:`TransactionError` to retry the transaction if the table has
not been locked at the start.
.. method:: Transaction.set_current_transaction(transaction)
2010-08-19 22:01:12 +02:00
2022-03-27 16:06:09 +02:00
Add a specific ``transaction`` on the top of the transaction stack.
A transaction is commited or rollbacked only when its last reference is
popped from the stack.
2010-08-19 22:01:12 +02:00
2022-12-26 15:05:56 +01:00
.. method:: Transaction.new_transaction([autocommit[, readonly, \**extras]])
2010-08-19 22:01:12 +02:00
2022-03-27 16:06:09 +02:00
Create a new transaction with the same database, user and context as the
original transaction and adds it to the stack of transactions.
2010-08-19 22:01:12 +02:00
.. method:: Transaction.commit()
2022-03-27 16:06:09 +02:00
Commit the transaction and all data managers associated.
.. method:: Transaction.rollback()
2022-03-27 16:06:09 +02:00
Rollback the transaction and all data managers associated.
.. method:: Transaction.join(datamanager)
2022-03-27 16:06:09 +02:00
Register in the transaction a data manager conforming to the `Two-Phase
Commit protocol`_.
2022-03-27 16:06:09 +02:00
This method returns the registered datamanager.
It could be a different yet equivalent (in term of python equality)
datamanager than the one passed to the method.
.. method:: Transaction.atexit(func, \*args, \*\*kwargs)
2022-03-27 16:06:09 +02:00
Register a function to be executed upon normal transaction termination.
The function can not use the current transaction because it will be already
committed or rollbacked.
.. function:: check_access([func])
When called with a function, it decorates the function to be executed with
check of access rights.
Otherwise it returns a `context manager`_ that check access rights until
exiting.
.. function:: without_check_access([func])
When called with a function, it decorates the function to be executed
without check of access rights.
Otherwise it returns a `context manager`_ that disable check access rights
until exiting.
.. function:: active_records([func])
When called with a function, it decorates the function to be executed with
active test enabled.
Otherwise it returns a `context manager`_ that enable active test.
.. function:: inactive_records(func)
When called with a function, it decorates the function to be executed with
active test disabled.
Otherwise it returns a `context manager`_ that disables active test.
2010-08-19 22:01:12 +02:00
.. _`context manager`: http://docs.python.org/reference/datamodel.html#context-managers
.. _`PEP-0249`: https://www.python.org/dev/peps/pep-0249/
.. _`Two-Phase Commit protocol`: https://en.wikipedia.org/wiki/Two-phase_commit_protocol