-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction.py
More file actions
28 lines (20 loc) · 875 Bytes
/
transaction.py
File metadata and controls
28 lines (20 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import dataclasses
import typing_extensions
from sqlalchemy.engine.interfaces import IsolationLevel
from sqlalchemy.ext import asyncio as sa_async
@dataclasses.dataclass(kw_only=True, frozen=True, slots=True)
class Transaction:
session: sa_async.AsyncSession
isolation_level: IsolationLevel | None = None
async def __aenter__(self) -> typing_extensions.Self:
if self.isolation_level:
await self.session.connection(execution_options={"isolation_level": self.isolation_level})
if not self.session.in_transaction():
await self.session.begin()
return self
async def __aexit__(self, *args: object, **kwargs: object) -> None:
await self.session.close()
async def commit(self) -> None:
await self.session.commit()
async def rollback(self) -> None:
await self.session.rollback()