On Thu, Apr 5, 2018 at 7:12 AM, 'Brian Candler' via sqlalchemy
<sqlalchemy@googlegroups.com> wrote:
> The documentation doesn't seem to be 100% clear on this point: if you
> close() a session, can you continue to use it as if it were a fresh session?
> (Unlike say a closed filehandle)
>
> In other words, in a non-threaded application, is this a valid pattern?
>
> Session = sessionmaker(bind=engine)
> session = Session()
>
> def my_request():
>     # Each request uses the same session object but cleans it up at the end
>     try:
>         ....
>     finally:
>         session.close()
>
> As opposed to:
>
> def my_request():
>     # Each request uses a separate session object
>     session = Session()
>     try:
>         ....
>     finally:
>         session.close()
>
> The reason for asking is that there are times where it would be more
> convenient to create a single session up-front and use it where required,
> closing at the end of each unit of work, than have to pass around a new
> session through multiple levels of function calls.

currently the Session will allow this, close() just closes out all of
its current state and leaves it available to be used again like a
brand new session.  It's always worked this way so there's not really
any way I could change this now.    The only caveat to this is in the
case of failure modes in the Session, e.g. when some error has been
raised and the Session does not fully recover its state correctly,
meaning the Session might be unusable from that point forward...there
have probably been some issues like this in the past and I'd have to
search to find out what they were.

that said, you definitely do *not* want to share the same Session
through concurrent requests, I see you use the term "non-threaded", so
OK.

If this Session is a global object of some kind which is no longer
passed to functions, there's still not much difference between calling
.close() on it and just re-assigning that session global to a new
Session object.

>
> In particular, flask-sqlalchemy wants to create a single session (albeit a
> scoped session).

a scoped session is a whole different thing.   that's a registry of
many Session objects keyed to a threading.local object.   I'm not sure
what flask-sqlalchemy's request cleanup code is but I'd imagine they
are calling scoped_session.remove(), and not just
scoped_session.close(), which means you would get a new Session.



> Hence if I want to use the same models in non-Flask code,
> I need to reference that session and just close it at the end of each unit
> of work, rather than create a fresh session each time.

it shouldn't be a problem but I'm not sure what you are gaining by doing so.

>
> Thanks,
>
> Brian.
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to