Skip to main content

Spring-style transactional boundaries for SQLAlchemy async sessions.

Project description

sqlalchemy-transactional

Spring-style transactional boundaries for SQLAlchemy async sessions. Inspired by Spring Framework's @Transactional model.

Table of Contents

Quick Start

This quick start uses a FastAPI integration example to show transaction boundaries in a real application flow, not as isolated snippets. It assumes you already run FastAPI with an async_sessionmaker and want Spring-like declarative transaction boundaries in service methods.

from fastapi import FastAPI, Request
from pydantic import BaseModel
from sqlalchemy import text
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine

from sqlalchemy_transactional.asyncio import (
    current_session,
    sessionmaker_context,
    transactional,
)
from sqlalchemy_transactional.common import Propagation

app = FastAPI()
engine = create_async_engine("sqlite+aiosqlite:///app.db")
sessionmaker = async_sessionmaker(engine, expire_on_commit=False)


# 1) Bind async_sessionmaker once per request boundary
@app.middleware("http")
async def transactional_context_middleware(request: Request, call_next):
    async with sessionmaker_context(sessionmaker):
        return await call_next(request)


class CreateUserRequest(BaseModel):
    name: str


# 2) Put transaction boundaries on service methods
class UserService:
    @transactional  # default propagation = REQUIRED
    async def create_user(self, name: str) -> None:
        await self._validate_name(name)
        await self._insert_user(name)

    @transactional(Propagation.MANDATORY)
    async def _validate_name(self, name: str) -> None:
        result = await current_session().execute(
            text("SELECT 1 FROM users WHERE name = :name"),
            {"name": name},
        )
        if result.first() is not None:
            raise ValueError("name already exists")

    @transactional(Propagation.MANDATORY)
    async def _insert_user(self, name: str) -> None:
        await current_session().execute(
            text("INSERT INTO users (name) VALUES (:name)"),
            {"name": name},
        )


user_service = UserService()


# 3) Route/controller stays thin
@app.post("/users")
async def create_user(payload: CreateUserRequest) -> dict[str, str]:
    await user_service.create_user(payload.name)
    return {"status": "ok"}

Why Use This

  • Keep transaction plumbing out of service code.
  • Declare transaction scope at the function boundary.
  • Use familiar propagation semantics from Spring (REQUIRED, MANDATORY, etc.).
  • Apply isolation level declaratively when needed.
  • Make service methods easier to read, test, and review.

Propagation Modes

  • REQUIRED (default): Join an active transaction, or create one if none exists.
  • MANDATORY: Require an active transaction; raise an error if missing.
  • REQUIRES_NEW: Always execute in a new transaction.
  • NESTED: Use savepoint semantics inside an active transaction; otherwise behave like REQUIRED.

Isolation Level

Set isolation level at the decorator boundary:

@transactional(isolation_level="SERIALIZABLE")
async def run_settlement() -> None:
    await current_session().execute(...)

If isolation_level is omitted (None, the default), SQLAlchemy uses the engine/dialect default isolation behavior from your database driver.

Combine with propagation when needed:

@transactional(Propagation.REQUIRES_NEW, isolation_level="SERIALIZABLE")
async def write_critical_audit_log() -> None:
    await current_session().execute(...)

When isolation_level is applied:

  • The decorator creates a new transaction (REQUIRES_NEW, or REQUIRED / NESTED when no active transaction exists).

When isolation_level is not applied by this decorator:

  • The function joins an already active transaction (REQUIRED with an active transaction, MANDATORY, or NESTED with an active transaction).

Spring-to-FastAPI Mapping

Spring concept FastAPI + sqlalchemy-transactional
@Transactional on service methods @transactional on async service methods
@Transactional(propagation = REQUIRED) @transactional (default = Propagation.REQUIRED)
@Transactional(propagation = MANDATORY) @transactional(Propagation.MANDATORY)
Request filter/interceptor binds tx resources @app.middleware("http") + sessionmaker_context(sessionmaker)
Current tx-bound resource lookup current_session()

Contributing

Development workflow, checks, and test policies are defined in AGENTS.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_transactional-1.0.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_transactional-1.0.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_transactional-1.0.2.tar.gz.

File metadata

File hashes

Hashes for sqlalchemy_transactional-1.0.2.tar.gz
Algorithm Hash digest
SHA256 66c9a53f130ec1d57ef6f4499b4f764ee719ba8d4ccf86f9e71bf0d52b48e4b9
MD5 d7f708671d9ff90ca8bef50385dea7a6
BLAKE2b-256 9893a2e2653c22789030dd668f5ad8ae5a1243e0f08c362eef99673639e4d608

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_transactional-1.0.2.tar.gz:

Publisher: pypi-publish.yml on harryplusplus/sqlalchemy-transactional

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlalchemy_transactional-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_transactional-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3071b7cb970b12438538c70ccf6a2cbeec3330a92c021bfa609c79aff7c9b52d
MD5 e9045481c4013ea8eb47de736b0364d4
BLAKE2b-256 a3acec303fa115cae0e4bffa8dfabd71ccfe4e8ff55466a83f9d87c71d359d27

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_transactional-1.0.2-py3-none-any.whl:

Publisher: pypi-publish.yml on harryplusplus/sqlalchemy-transactional

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page