On May 20, 2014, at 9:48 PM, Jonathan Vanasco <[email protected]> wrote:

> in our system we have 2 Ssession objects:
> 
> * reader database
> * writer database
> 
> i'm wondering if it's possible to query the Session object for the following:
> 
> * has the Session established a connection to the database already ?
>   ie, has it undergone any activity, such as actually connecting and/or 
> emitting a `begin` statement
>   from my understanding, accessing a `.connection` object might connect to 
> the database
> 
> * has the Session loaded a specific item yet (via `get` or stored in the 
> internal stash)?
>   everything i've read in the API would actually try to load the item from 
> the database.
> 
> for the sake of transparency, i'm looking to optimize some database 
> interactions.  there are a few places where i may be able to save a db call 
> by seeing if we connected on the other handle and loaded that data already.

the best way to track transaction state is to use the after_transaction_create 
event or similar: 
http://docs.sqlalchemy.org/en/rel_0_9/orm/events.html#sqlalchemy.orm.events.SessionEvents.after_transaction_create

though you can also check in the transaction itself:

session.transaction._connections

for the "loaded" an item just look in session.identity_map, it's all there.

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to