1818def transaction (session : Session ) -> Generator [Session , None , None ]:
1919 """Context manager for synchronous database transactions.
2020
21- Automatically commits the transaction on successful completion or rolls back
22- on any exception. The original exception is wrapped in a TransactionError
23- to provide additional context while preserving the exception chain.
21+ Automatically commits the transaction on successful completion or
22+ rolls back on any exception. The original exception is wrapped in a
23+ TransactionError to provide additional context while preserving the
24+ exception chain.
2425
2526 Args:
2627 session: An active SQLModel Session instance
@@ -29,8 +30,9 @@ def transaction(session: Session) -> Generator[Session, None, None]:
2930 Session: The same session instance for use within the context
3031
3132 Raises:
32- TransactionError: Wraps any exception that occurs during the transaction,
33- with the original exception available via __cause__
33+ TransactionError: Wraps any exception that occurs during
34+ the transaction, with the original exception available
35+ via __cause__
3436
3537 Example:
3638 Basic usage with automatic commit:
@@ -43,7 +45,9 @@ def transaction(session: Session) -> Generator[Session, None, None]:
4345 >>> with Session(engine) as session:
4446 ... with transaction(session) as tx:
4547 ... user = write_row(User(name="Alice"), tx)
46- ... update_row(user.id, {"email": "alice@example.com"}, User, tx)
48+ ... update_row(
49+ ... user.id, {"email": "alice@example.com"}, User, tx
50+ ... )
4751 ... # Automatically commits here if no exceptions
4852
4953 Error handling with automatic rollback:
@@ -93,18 +97,22 @@ async def a_transaction(
9397) -> AsyncGenerator [AsyncSession , None ]:
9498 """Context manager for asynchronous database transactions.
9599
96- Asynchronous version of the transaction() context manager. Provides the same
97- automatic commit/rollback behavior for async database operations.
100+ Asynchronous version of the transaction() context manager. Provides
101+ the same automatic commit/rollback behavior for async database
102+ operations.
98103
99104 Args:
100- session: An active AsyncSession instance from sqlmodel.ext.asyncio.session
105+ session: An active AsyncSession instance from
106+ sqlmodel.ext.asyncio.session
101107
102108 Yields:
103- AsyncSession: The same session instance for use within the async context
109+ AsyncSession: The same session instance for use within the
110+ async context
104111
105112 Raises:
106- TransactionError: Wraps any exception that occurs during the transaction,
107- with the original exception available via __cause__
113+ TransactionError: Wraps any exception that occurs during
114+ the transaction, with the original exception available
115+ via __cause__
108116
109117 Example:
110118 Basic async usage with automatic commit:
@@ -117,8 +125,13 @@ async def a_transaction(
117125 >>> engine = create_async_engine("sqlite+aiosqlite:///database.db")
118126 >>> async with AsyncSession(engine) as session:
119127 ... async with a_transaction(session) as tx:
120- ... user = await a_write_row(User(name="Alice"), tx)
121- ... await a_update_row(user.id, {"email": "alice@example.com"}, User, tx)
128+ ... user = await a_write_row(
129+ ... User(name="Alice"), tx
130+ ... )
131+ ... await a_update_row(
132+ ... user.id, {"email": "alice@example.com"},
133+ ... User, tx
134+ ... )
122135 ... # Automatically commits here if no exceptions
123136
124137 Error handling with automatic rollback:
0 commit comments