Overview
Transactions are especially useful when:- Creating many data objects and attributes at once
- Performing extraction operations that produce multiple related records
- Needing atomic rollback on failure
- Optimizing performance for bulk operations
Python Usage
In Python, use thebatch_transaction() context manager:
Transaction Operations
TheTransactionContext provides accessors that mirror the standard data accessors:
| Accessor | Method | Description |
|---|---|---|
tx.data_objects | create(input) | Queue a data object creation |
tx.data_objects | update(id, updates) | Queue a data object update |
tx.data_objects | delete(id) | Queue a data object deletion |
tx.data_attributes | create(obj_id, input) | Queue an attribute creation |
tx.data_attributes | update(id, updates) | Queue an attribute update |
tx.data_attributes | delete(id) | Queue an attribute deletion |
tx.data_attributes | set_value(id, value) | Queue a value update |
tx.data_attributes | set_confidence(id, confidence) | Queue a confidence update |
ID Resolution
When you create a data object within a transaction, it returns a record with a temporary ID. You can use this temporary ID to create child objects or attributes within the same transaction. The IDs are resolved to real database IDs when the transaction is committed.Error Handling
If an exception occurs within the transaction block, all queued operations are discarded:TypeScript Usage
In TypeScript, use thetransaction() method with an async callback:
Performance
Transactions provide significant performance benefits for bulk operations:- Without transactions: Each create/update/delete is a separate FFI/WASM call
- With transactions: All operations are batched into a single call
Best Practices
- Use transactions for bulk operations: Any time you’re creating more than a few data objects or attributes, wrap them in a transaction.
- Keep transactions focused: Don’t mix unrelated operations in the same transaction.
- Handle errors: Wrap transaction blocks in try/except (Python) or try/catch (TypeScript) to handle failures gracefully.
- Check operation count: Use
tx.operation_count(Python) ortx.operationCount(TypeScript) to verify the expected number of operations before committing.
