#SQLAlchemy Session Management

1 messages · Page 1 of 1 (latest)

narrow geyser
#

I'm currently migrating from flask with Flask-SQLAlchemy, and really like how it's easy to hang onto a session that's scoped to each request. The example code I saw on the website all relies on passing the session down from the request handler.

What's the right way to create a class that holds onto the session object and works across requests? I'd love to write code like this:

def add_user(self):
  self.session.add(...)
  self.session.commit()
calm mountainBOT
#
Notes for SQLAlchemy Session Management
At your assistance

@narrow geyser

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

tidal narwhal
#

Well, for a while, SQLAlchemy recommended against using scoped sessions. It looks like they've refined the verbiage to use "flask-sqlalchemy" where this pattern is more prevalent

whole pebbleBOT
#

advanced_alchemy/extensions/litestar/plugins/init/config/asyncio.py line 181

def provide_session(self, state: State, scope: Scope) -> AsyncSession:
tidal narwhal
#

Also, the repositories work like this essentially without the need for a scoped session

whole pebbleBOT
#

src/app/cli/commands.py line 176

async with alchemy.get_session() as db_session:

src/app/cli/commands.py line 136

async with UserService.new() as users_service:
tidal narwhal
#

That said, I'd still think about whether you need scoped sessions or not

narrow geyser
#

Ok, thanks

#

What are the downsides to scoped sessions?

tidal narwhal
#

The first thing that comes to mind is transaction management is a bit obfuscated.
Secondly, in the context of a web framework, the session is likely meant to be scoped to the request. This still means that the commit/rollback etc still has to be managed each request. This is what the Advanced Alchemy plugin is already doing for you.

#

That said, the repositories and services do already work with scoped sessions

narrow geyser
#

So the async with alchemy.get_session() as db_session: line handles getting a session scoped to the active request?

tidal narwhal
whole pebbleBOT
#

src/app/config/app.py line 28

alchemy = SQLAlchemyAsyncConfig(
tidal narwhal
#

so, this configuration will give you a new session for each request that is already injected into the route, as. well as a commit for the transaction on each request if there is no exception raised (in this case, a rollback is issued).

whole pebbleBOT
#

src/app/asgi.py line 43

plugins.alchemy,
tidal narwhal
whole pebbleBOT
#

src/app/domain/system/controllers.py line 45

settings = get_settings()
tidal narwhal
#

this gives you a fresh session each request

#

I'll do a bit of research to see if there's an easy way to give the option of a scoped session out of the box in AA. But, I can't promise anything.

narrow geyser
#

Ok, I think I'll try refactoring to get the session for each route through db_session, the advanced alchemy code looks a lot cleaner than the other examples I saw