class scoped_session(ScopedSessionMixin):
Provides scoped management of .Session
objects.
See :ref:`unitofwork_contextual` for a tutorial.
Note
When using :ref:`asyncio_toplevel`, the async-compatible
_asyncio.async_scoped_session
class should be
used in place of .scoped_session
.
Method | __init__ |
Construct a new .scoped_session . |
Method | query_property |
return a class property which produces a _query.Query object against the class and the current .Session when called. |
Method | remove |
Dispose of the current .Session , if present. |
Instance Variable | session_factory |
No summary |
Class Variable | _support_async |
Undocumented |
Instance Variable | registry |
Undocumented |
Inherited from ScopedSessionMixin
:
Method | __call__ |
Return the current .Session , creating it using the .scoped_session.session_factory if not present. |
Method | configure |
reconfigure the .sessionmaker used by this .scoped_session . |
Property | _proxied |
Undocumented |
.scoped_session
.Parameters | |
session_factory | a factory to create new .Session
instances. This is usually, but not necessarily, an instance
of .sessionmaker . |
scopefunc | optional function which defines
the current scope. If not passed, the .scoped_session
object assumes "thread-local" scope, and will use
a Python threading.local() in order to maintain the current
.Session . If passed, the function should return
a hashable token; this token will be used as the key in a
dictionary in order to store and retrieve the current
.Session . |
return a class property which produces a _query.Query
object
against the class and the current .Session
when called.
e.g.:
Session = scoped_session(sessionmaker()) class MyClass(object): query = Session.query_property() # after mappers are defined result = MyClass.query.filter(MyClass.name=='foo').all()
Produces instances of the session's configured query class by default. To override and use a custom implementation, provide a query_cls callable. The callable will be invoked with the class's mapper as a positional argument and a session keyword argument.
There is no limit to the number of query properties placed on a class.
Dispose of the current .Session
, if present.
This will first call .Session.close
method
on the current .Session
, which releases any existing
transactional/connection resources still being held; transactions
specifically are rolled back. The .Session
is then
discarded. Upon next usage within the same scope,
the .scoped_session
will produce a new
.Session
object.
session_factory
provided to __init__
is stored in this
attribute and may be accessed at a later time. This can be useful when
a new non-scoped .Session
or _engine.Connection
to the
database is needed.