On Aug 11, 2011, at 5:46 AM, Massi wrote:

> Hi everyone,
> 
> in my script I work with two different sessions (say session1 and
> session2) bounded to the same database and, consequently, with ORM
> objects obtained with queries issued on them. It can occur that an
> object related to session1 change a value of the corresponding mapped
> table record and, on the other hand, there exist an object related to
> session2 mapped to the same table record. In this case the second
> object becomes misaligned with respect to the mapped table. So, my
> question is...which is the best approach to handle this situation? Can
> a session object somehow become aware that it must be synchronized/
> refreshed?
> Thanks in advance for your help!

The Session operates in a transaction which is assumed to be isolated from all 
other transactions.     in practice, the level of isolation between 
transactions of course varies, but the Session model remains constant, in that 
data from other transactions is assumed to be available once a new local 
transaction begins - so Session expires all data at the end of a transaction.

So the short answer is call commit() on session1 as well as session2.   
session1 will expire all of its data and start a new transaction; session 2 
will also flush its changes and commit the transaction, so that those changes 
are now viewable by other transactions.     "Expiring" from the Session 
perspective means that all requests for database-bound data will emit new SQL.  
 See http://www.sqlalchemy.org/docs/orm/session.html#refreshing-expiring .

Note that both SQLAlchemy *as well as* the isolation of the transaction itself 
on the database side play a role in "concealing" concurrent changes.    
Expiring the data in the SQLAlchemy session alone, which you can do via 
expire() or expire_all(), will still not show any changes from other sessions, 
if the database is isolating those transactions from each other until after 
commit.

An overview of isolation is at:

http://en.wikipedia.org/wiki/Isolation_%28database_systems%29






> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to