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. Mike Bayer repo owner

    it's not underscored, so it's public. I probably refer to it in some wiki recipe or blog post somewhere. Feel fee to document it.

  2. 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?

  3. Mike Bayer repo owner

    sphinx is able to get class attributes (usually...with some v. frustrating failures), so if you docstring underneath that should work

  4. jvanasco reporter

    Great! Thanks. I see that form being used on some package-level variables, and know what to look for in the Sphinx docs.

  5. Log in to comment