Feature Request - ScopedSession.session_factory as an API method

Issue #3350 resolved
jvanasco
created an issue

I'm requesting that ScopedSession.session_factory become a public API method.

This is currently implemented, but hidden and undocumented.

Rationale / use-case:

We have a transaction-safe PostgreSQL application. There exists some tasks that require a separate database connection to operate independent of the transaction (generally things like logging various bits of information, such as AmazonS3 Activity, Failures due to Ratelimits, some error logging).

This is often handled with a separate database setup, but that was a bit painful to handle in a situation... and after a bit of poking around on the internals, it seems that this works:

dbSessionAlternate = dbSession.session_factory()
dbSessionAutocommit = dbSession.session_factory(autocommit = True)

Originally I hoped that this would work, but they don't:

dbSessionA = dbSession()
dbSessionB = dbSession(autocommit = True)

dbSessionA creates a new Session, but it's underlying connection is local to the scope/thead ( ie, dbSession.connection() == dbSessionA.connection() )

dbSessionB raises an error because it is trying to duplicate the 'scoped_session', not just the underlying session,

Comments (7)

  1. jvanasco reporter

    Wonderful!

    It's implemented as a class attribute. I couldn't find any references for how that should be documented -- most similar things are class methods or properties.

    The only approach I can think of is adding a line to the class and init docstings. Is there a better way?

  2. Log in to comment