Reference
shared.transaction.CommitInTransactionError ¶
Bases: TransactionError
Exception raised when an explicit commit is detected in a transaction block
shared.transaction.Propagation ¶
Bases: Enum
Transaction propagation behaviors
NESTED
class-attribute
instance-attribute
¶
Create a new nested transaction within the current one
READ_ONLY
class-attribute
instance-attribute
¶
Create a read-only transaction using the read-only engine
REQUIRED
class-attribute
instance-attribute
¶
Use existing transaction if available, create a new one if not
REQUIRES_NEW
class-attribute
instance-attribute
¶
Always create a new transaction, suspend current if it exists
shared.transaction.ReadOnlySessionFactory ¶
Bases: ISessionFactory
Factory to build read-only sessions
Source code in shared/transaction/session.py
__call__ ¶
Source code in shared/transaction/session.py
shared.transaction.SessionFactory ¶
Bases: ISessionFactory
Factory to build sessions
Source code in shared/transaction/session.py
__call__ ¶
Source code in shared/transaction/session.py
shared.transaction.TransactionError ¶
Bases: Exception
Base class for transaction exceptions
shared.transaction.UnitOfWork ¶
shared.transaction.context ¶
TransactionContext ¶
TransactionContext(
use_global_session=USE_GLOBAL_SESSION,
session_factory=None,
read_only_session_factory=None,
producer=None,
)
State management for the transaction context
Session management¶
Maintain a session stack, keeping track of nested transaction contexts.
Commit guard¶
Protect against commit inside a transaction context while allowing explicit bypass.
Event management¶
Maintain events associated with a particular transaction context. Allow to dispatch them when the associated transaction is committed. Event state machine is as follow:
stateDiagram-v2
[*] --> Pending: Register
Pending --> Approved: Approve
Pending --> [*]: Clear
Approved --> [*]: Dispatch
- Register an event to be dispatched on success of the current transaction (public)
- Approve when the context exits without exceptions (internal)
- Clear when the context exits with exception (internal)
- Dispatch when the associated transaction is committed (internal)
Source code in shared/transaction/context.py
approve_pending_events ¶
Approve all pending events
Source code in shared/transaction/context.py
clear_pending_events ¶
Clear all pending events
Source code in shared/transaction/context.py
commit_guard ¶
Context manager setting a commit guard
Source code in shared/transaction/context.py
commit_guard_bypass ¶
Context manager to allow bypassing the commit guard
Source code in shared/transaction/context.py
create_session ¶
Add a new session on the stack
Source code in shared/transaction/context.py
pop_session ¶
Pop a session from the stack
Source code in shared/transaction/context.py
publish_events ¶
Dispatch all approved events
Source code in shared/transaction/context.py
read_only_session_factory
instance-attribute
¶
register_event ¶
Register an event
Source code in shared/transaction/context.py
remove_global_session ¶
Close the global session and make it ready to be created again
Source code in shared/transaction/context.py
get_transaction_context ¶
Get the transaction context, creating it if needed
mock_transaction_context ¶
Utility to temporarily change the behavior of the context
This is useful for testing purposes.
Source code in shared/transaction/context.py
shared.transaction.core ¶
Propagation ¶
Bases: Enum
Transaction propagation behaviors
NESTED
class-attribute
instance-attribute
¶
Create a new nested transaction within the current one
READ_ONLY
class-attribute
instance-attribute
¶
Create a read-only transaction using the read-only engine
REQUIRED
class-attribute
instance-attribute
¶
Use existing transaction if available, create a new one if not
REQUIRES_NEW
class-attribute
instance-attribute
¶
Always create a new transaction, suspend current if it exists
register_event ¶
transaction ¶
Provide a transactional context manager for database operations.
This function creates and manages database transactions with various propagation
behaviors. It integrates with the existing current_session for backward
compatibility while providing explicit transaction management.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
propagation
|
Propagation
|
The transaction propagation behavior. Defaults to REQUIRED. - REQUIRED: Use existing transaction if available, create new if not - REQUIRES_NEW: Always create a new transaction, suspend current if exists - NESTED: Create a nested transaction within the current one - READ_ONLY: Create a read-only transaction using read-only engine |
REQUIRED
|
force_manage
|
bool
|
Whether to take control of existing transactions. Defaults to False. When True, will commit/rollback existing transactions that weren't started by this context manager. Use with caution during migration from legacy code. |
False
|
Returns:
| Type | Description |
|---|---|
Iterator[Session]
|
Iterator[Session]: A SQLAlchemy session within the transaction context |
Raises:
| Type | Description |
|---|---|
InReadOnlyTransactionError
|
When trying to nest transactions in read-only context |
NotImplementedError
|
When using an unsupported propagation type |
Examples:
Basic usage with context manager:
Using different propagation behaviors:
>>> with transaction(propagation=Propagation.REQUIRES_NEW) as session:
... session.add(User(name="Alice")) # always in its own transaction
>>> with transaction(propagation=Propagation.READ_ONLY) as session:
... users = session.scalars(select(User)).all() # read-only operations
Force managing existing transactions (migration helper):
>>> with transaction(force_manage=True) as session:
... session.add(User(name="Bob")) # will commit existing transaction
Note
- Transactions are automatically committed on successful context exit
- Transactions are automatically rolled back on exceptions
- Nested contexts share the same transaction unless REQUIRES_NEW is used
- Events registered via
register_event()are dispatched on successful commit - In dry-run mode, all transactions are rolled back regardless of success
- Read-only transactions are always rolled back to prevent any data changes
Source code in shared/transaction/core.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
transactional ¶
Decorator that provides transactional context around a function
The decorated function must accept a session keyword arguments that will be
injected by the decorator.
@transactional ... def my_function(session: Session, arg1, arg2): ... # do something with the session
Source code in shared/transaction/core.py
shared.transaction.exception ¶
CommitInTransactionError ¶
Bases: TransactionError
Exception raised when an explicit commit is detected in a transaction block
InReadOnlyTransactionError ¶
Bases: TransactionError
Exception raised when trying to acquire a non read-only transaction context within a read-only one
TransactionError ¶
Bases: Exception
Base class for transaction exceptions
shared.transaction.mock_transaction_context ¶
Utility to temporarily change the behavior of the context
This is useful for testing purposes.
Source code in shared/transaction/context.py
shared.transaction.read_only_session_factory
module-attribute
¶
shared.transaction.register_event ¶
shared.transaction.session ¶
ReadOnlySessionFactory ¶
Bases: ISessionFactory
Factory to build read-only sessions
Source code in shared/transaction/session.py
__call__ ¶
Source code in shared/transaction/session.py
SessionFactory ¶
Bases: ISessionFactory
Factory to build sessions
Source code in shared/transaction/session.py
__call__ ¶
Source code in shared/transaction/session.py
shared.transaction.session_registry
module-attribute
¶
shared.transaction.transaction ¶
Provide a transactional context manager for database operations.
This function creates and manages database transactions with various propagation
behaviors. It integrates with the existing current_session for backward
compatibility while providing explicit transaction management.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
propagation
|
Propagation
|
The transaction propagation behavior. Defaults to REQUIRED. - REQUIRED: Use existing transaction if available, create new if not - REQUIRES_NEW: Always create a new transaction, suspend current if exists - NESTED: Create a nested transaction within the current one - READ_ONLY: Create a read-only transaction using read-only engine |
REQUIRED
|
force_manage
|
bool
|
Whether to take control of existing transactions. Defaults to False. When True, will commit/rollback existing transactions that weren't started by this context manager. Use with caution during migration from legacy code. |
False
|
Returns:
| Type | Description |
|---|---|
Iterator[Session]
|
Iterator[Session]: A SQLAlchemy session within the transaction context |
Raises:
| Type | Description |
|---|---|
InReadOnlyTransactionError
|
When trying to nest transactions in read-only context |
NotImplementedError
|
When using an unsupported propagation type |
Examples:
Basic usage with context manager:
Using different propagation behaviors:
>>> with transaction(propagation=Propagation.REQUIRES_NEW) as session:
... session.add(User(name="Alice")) # always in its own transaction
>>> with transaction(propagation=Propagation.READ_ONLY) as session:
... users = session.scalars(select(User)).all() # read-only operations
Force managing existing transactions (migration helper):
>>> with transaction(force_manage=True) as session:
... session.add(User(name="Bob")) # will commit existing transaction
Note
- Transactions are automatically committed on successful context exit
- Transactions are automatically rolled back on exceptions
- Nested contexts share the same transaction unless REQUIRES_NEW is used
- Events registered via
register_event()are dispatched on successful commit - In dry-run mode, all transactions are rolled back regardless of success
- Read-only transactions are always rolled back to prevent any data changes
Source code in shared/transaction/core.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
shared.transaction.transactional ¶
Decorator that provides transactional context around a function
The decorated function must accept a session keyword arguments that will be
injected by the decorator.
@transactional ... def my_function(session: Session, arg1, arg2): ... # do something with the session