r/FastAPI • u/RealnessKept • Jan 13 '26
Question Creating Connections in ContextVar
I am wanting to create connections in ContextVars, but I don't know if it is safe.
I am using SQLAlchemy and AsyncPG. I also have a notion of a Database Access Object, which is essentially a singleton class that manages database connections.
Many of my endpoints follow this pattern at the moment:
@router.post("", status_code=status.HTTP_201_CREATED)
@requires_auth(MSMRole.MarketManager)
async def create_market_manager_contact(contact: Contact):
"""Creates a contact for the authenticated market manager."""
account_id = get_account_id()
async with mm_db.engine.begin() as conn:
address_id = None
if contact.address:
address_id = await mm_db.upsert_address(contact.address, conn=conn)
contact_id = await mm_db.create_market_manager_contact(
conn=conn,
contact=contact,
market_manager_id=account_id,
address_id=address_id
)
return {"contact_id": contact_id}
My requires_auth validates a JWT and a session (if JWT is invalid) then creates a ContextVar for the identifying information of the person who called the endpoint. I was thinking of adding a connection kind of like this:
async def connection_scope(
role: MSMRole,
account_id: UUID,
commit: bool = True
):
"""Context manager for database connection lifecycle."""
dao = _get_dao_for_role(role)
async with dao.engine.begin() as conn:
await conn.execute(sqlalchemy.text(f"SET LOCAL ROLE {dao.role}"))
context = ConnectionContext(
connection=conn,
dao=dao,
role=role,
account_id=account_id
)
set_connection_context(context)
try:
yield context
if not commit:
pass # Transaction will auto-rollback if not committed
except Exception as e:
raise # Transaction will auto-rollback on exception
finally:
clear_connection_context()
And finally how to use it:
@asynccontextmanager
async def connection_scope_from_auth():
"""Convenience wrapper that gets role and account_id from auth context."""
from src.utilities.security.fastapi import get_auth_context
auth_context = get_auth_context()
async with connection_scope(
role=auth_context.role,
account_id=auth_context.account_id
) as ctx:
yield ctx
I am wondering if there are issues with this pattern when it comes to threads and blocking. I don't see many people do it this way, so it makes me hesitant. Do let me know your thoughts, or if you know of a better way!
View all comments
Show parent comment
1
u/AmadeusBeta Jan 14 '26
Yeah you update the dependency to be on request level/router level so that the approach is more fastapi like.