Module¶
pgtransaction.atomic
¶
atomic(
using: str | None = None,
savepoint: bool = True,
durable: bool = False,
*,
isolation_level: Literal["SERIALIZABLE"] = ...,
retry: int | None = None,
read_mode: Literal["READ ONLY"] | None = None,
deferrable: Literal["DEFERRABLE"] | None = None
) -> Atomic
atomic(
using: str | None = None,
savepoint: bool = True,
durable: bool = False,
*,
isolation_level: (
Literal["READ COMMITTED", "REPEATABLE READ", "SERIALIZABLE"] | None
) = None,
retry: int | None = None,
read_mode: Literal["READ WRITE", "READ ONLY"] | None = None,
deferrable: Literal["DEFERRABLE", "NOT DEFERRABLE"] | None = None
) -> Atomic
atomic(
using: str | None | _C = None,
savepoint: bool = True,
durable: bool = False,
*,
isolation_level: (
Literal["READ COMMITTED", "REPEATABLE READ", "SERIALIZABLE"] | None
) = None,
retry: int | None = None,
read_mode: Literal["READ WRITE", "READ ONLY"] | None = None,
deferrable: Literal["DEFERRABLE", "NOT DEFERRABLE"] | None = None
) -> Atomic | _C
Extends django.db.transaction.atomic with PostgreSQL functionality.
Allows one to dynamically set transaction characteristics when opening a transaction, including isolation level, read mode, and deferrability. Also supports specifying a retry policy for when an operation in that transaction results in a Postgres locking exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
using
|
str | None | _C
|
The database to use. |
None
|
savepoint
|
bool
|
If |
True
|
durable
|
bool
|
If |
False
|
isolation_level
|
Literal['READ COMMITTED', 'REPEATABLE READ', 'SERIALIZABLE'] | None
|
The isolation level we wish to be
used for the duration of the transaction. If passed in
as None, the current isolation level is used. Otherwise,
we must choose from |
None
|
retry
|
int | None
|
An integer specifying the number of attempts
we want to retry the entire transaction upon encountering
the settings-specified psycogp2 exceptions. If passed in as
None, we default to using the settings-specified retry
policy defined by |
None
|
read_mode
|
Literal['READ WRITE', 'READ ONLY'] | None
|
The read mode for the transaction. Must be one of
|
None
|
deferrable
|
Literal['DEFERRABLE', 'NOT DEFERRABLE'] | None
|
Whether the transaction is deferrable. Must be one of
|
None
|
Example
Since pgtransaction.atomic inherits from django.db.transaction.atomic, it
can be used in exactly the same manner. Additionally, when used as a
context manager or a decorator, one can use it to specify transaction
characteristics. For example:
import pgtransaction
with pgtransaction.atomic(isolation_level=pgtransaction.REPEATABLE_READ):
# Transaction is now REPEATABLE READ for the duration of the block
...
# Use READ ONLY mode with SERIALIZABLE isolation
with pgtransaction.atomic(
isolation_level=pgtransaction.SERIALIZABLE,
read_mode=pgtransaction.READ_ONLY
):
# Transaction is now SERIALIZABLE and READ ONLY
...
# Use DEFERRABLE with SERIALIZABLE and READ ONLY
with pgtransaction.atomic(
isolation_level=pgtransaction.SERIALIZABLE,
read_mode=pgtransaction.READ_ONLY,
deferrable=pgtransaction.DEFERRABLE
):
# Transaction is now SERIALIZABLE, READ ONLY, and DEFERRABLE
...
Note that setting transaction modes in a nested atomic block is permitted as long as no queries have been made.
Example
When used as a decorator, one can also specify a retry argument. This
defines the number of times the transaction will be retried upon encountering
the exceptions referenced by settings.PGTRANSACTION_RETRY_EXCEPTIONS,
which defaults to
(psycopg.errors.SerializationFailure, psycopg.errors.DeadlockDetected).
For example:
@pgtransaction.atomic(retry=3)
def update():
# will retry update function up to 3 times
# whenever any exception in settings.PGTRANSACTION_RETRY_EXCEPTIONS
# is encountered. Each retry will open a new transaction (after
# rollback the previous one).
Attempting to set a non-zero value for retry when using pgtransaction.atomic
as a context manager will result in a RuntimeError.
Source code in pgtransaction/transaction.py
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |