Skip to main content

Tables.data_transaction(scenario_name=None, *, allow_nested=True, tables=None)

Create a data transaction to batch several data loading operations.
  • It is more efficient than doing each load() one after the other, especially when using load_async() to load data concurrently in multiple tables.
  • It avoids possibly incorrect intermediate states (e.g. if loading some new data requires dropping existing rows first).
  • If an exception is raised during a data transaction, it will be rolled back and the changes made until the exception will be discarded.
Data transactions cannot be mixed with:
  • Parameters:
    • allow_nested (bool) – Whether to allow starting this transaction inside an already running one. When False, an error will be raised if this transaction is started while another transaction is already running, regardless of that outer transaction’s value of allow_nested. The benefit of passing False is that changes made in this transaction are guaranteed, if not rolled back, to be visible to the statements outside the transaction. The drawback is that it prevents splitting transaction steps in small composable functions. When nested transactions are allowed, changes made by inner transactions contribute transparently to the outer transaction and will only be committed when the outer transaction’s context exits.
    • scenario_name (str | None) – The name of the source scenario impacted by all the table operations inside the transaction.
    • tables (Set *[*HasIdentifier *[*TableIdentifier ] | TableIdentifier ] | None) – The tables that can be affected by this transaction. Tables transitively joined with these tables will be locked too. Transactions locking disjoint sets of tables execute concurrently. When None, all tables are locked.
  • Return type: AbstractContextManager[None]

Example

If an exception is raised during a data transaction, the changes made until the exception will be rolled back.
Loading data concurrently in multiple tables:
Nested transactions allowed:
Nested transactions not allowed:
Restricting the transaction to a subset of tables:
Nested transactions must specify a subset of the outer transaction’s tables: