diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/sqlalchemy/orm/scoping.py | 33 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/session.py | 11 |
2 files changed, 36 insertions, 8 deletions
diff --git a/lib/sqlalchemy/orm/scoping.py b/lib/sqlalchemy/orm/scoping.py index fff17ee14..e827bc5b8 100644 --- a/lib/sqlalchemy/orm/scoping.py +++ b/lib/sqlalchemy/orm/scoping.py @@ -42,26 +42,45 @@ class scoped_session(object): else: self.registry = ThreadLocalRegistry(session_factory) - def __call__(self, **kwargs): - """Return the current :class:`.Session`.""" - if kwargs: - scope = kwargs.pop('scope', False) + def __call__(self, **kw): + """Return the current :class:`.Session`, creating it + using the session factory if not present. + + :param \**kw: Keyword arguments will be passed to the + session factory callable, if an existing :class:`.Session` + is not present. If the :class:`.Session` is present and + keyword arguments have been passed, :class:`.InvalidRequestError` + is raised. + + """ + if kw: + scope = kw.pop('scope', False) if scope is not None: if self.registry.has(): raise sa_exc.InvalidRequestError( "Scoped session is already present; " "no new arguments may be specified.") else: - sess = self.session_factory(**kwargs) + sess = self.session_factory(**kw) self.registry.set(sess) return sess else: - return self.session_factory(**kwargs) + return self.session_factory(**kw) else: return self.registry() def remove(self): - """Dispose of the current contextual session.""" + """Dispose of the current :class:`.Session`, if present. + + This will first call :meth:`.Session.close` method + on the current :class:`.Session`, which releases any existing + transactional/connection resources still being held; transactions + specifically are rolled back. The :class:`.Session` is then + discarded. Upon next usage within the same scope, + the :class:`.scoped_session` will produce a new + :class:`.Session` object. + + """ if self.registry.has(): self.registry().close() diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 96a6983f8..e0f79cd8a 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -2041,11 +2041,20 @@ class sessionmaker(_SessionClassMethods): with an existing :class:`.sessionmaker` factory before it is first used:: + # application starts Session = sessionmaker() - Session.configure(bind=create_engine('sqlite:///foo.db')) + + # ... later + engine = create_engine('sqlite:///foo.db') + Session.configure(bind=engine) sess = Session() + .. seealso: + + :ref:`session_getting` - introductory text on creating + sessions using :class:`.sessionmaker`. + """ def __init__(self, bind=None, class_=Session, autoflush=True, |
