On Dec 19, 2011, at 1:04 AM, Jackson, Cameron wrote: > OK, I think I understand. Just to make sure, how about this example: > > from SomeModule import Session, Foo > session1 = Session() > session2 = Session() > > data1 = session1.query(Foo).all() > data2 = session2.query(Foo).all() > > data1[0].bar = 'Baz' > data1.append(Foo) > session1.add(data1[-1]) > session1.commit() > > session2.rollback() > data2 = session2.query(Foo).all() > > After all of that, data2[0].bar should equal 'Baz', and data2[-1] should be > the new Foo that we created and added to data[1], correct? In other words, > the contents of data1 and data2 should be identical, right?
well you wouldn't be appending the "Foo" class so I assume there's some paraphrasing going on, but the general idea that s1 is committed, then s2 rolled back, s2 would emit SQL for everything yes. "identical" as I said would go as far as ORM-mapped attributes. Using echo='debug' with your engine is the best way to sanity check this stuff. > > Cheers, > > Cameron Jackson > Engineering Intern > Air Operations > Thales Australia > Thales Australia Centre, WTC Northbank Wharf, Concourse Level, > Siddeley Street, Melbourne, VIC 3005, Australia > Tel: +61 3 8630 4591 > cameron.jack...@thalesgroup.com.au | www.thalesgroup.com.au > From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On > Behalf Of Michael Bayer > Sent: Monday, 19 December 2011 12:51 PM > To: sqlalchemy@googlegroups.com > Subject: Re: [sqlalchemy] Multiple sessions and data conflicts. > > > On Dec 18, 2011, at 8:25 PM, Jackson, Cameron wrote: > > > Here is a simplified version of my scenario: > My application has two session objects. One is being used for editing certain > data, and the other is reading this data, doing some calculations, and > displaying the results. I've set up a callback kind of system so that > whenever the first session does a commit, a method is called that does a > rollback on the second session, and redoes the calculations and display. > I've had bugs crop up where the data I get from querying the second session > object doesn't match what I know is in the database, but I haven't been able > to reproduce them consistently yet. So my questions are: > 1. After a session.rollback(), should the data obtained from that > session's queries match what's in the DB now? Or what was in the DB when the > session was created? The docs are not clear on this. > 2. If a rollback is not enough to guarantee that the data from querying > a session is up to date, then what is? Should I just create a new session > entirely? > > These are the comprehensive docs on rollback: > > http://www.sqlalchemy.org/docs/orm/session.html#rolling-back > > > A rollback expires all attributes that are database mapped, on objects that > are persistent within the session. Touching any of these attributes > subsequent to the expire will cause them to emit SQL to get the new value > back. > > You might not see what you expect if A. you're accessing an object that isn't > persistent within that session, such as one that was "new" before the > rollback, as these get evicted after rollback, or B. you're looking at your > own attributes which aren't directly database mapped. > > As far as "what's in the DB", a new transaction starts after rollback, and > that's against whatever has been committed to that DB. If your other > transaction hasn't committed then you wouldn't see its data. > > > > > > cheers, > Cam > Cameron Jackson > Engineering Intern > Air Operations > Thales Australia > Thales Australia Centre, WTC Northbank Wharf, Concourse Level, > Siddeley Street, Melbourne, VIC 3005, Australia > Tel: +61 3 8630 4591 > cameron.jack...@thalesgroup.com.au | www.thalesgroup.com.au > ------------------------------------------------------------------------- > DISCLAIMER: This e-mail transmission and any documents, files and previous > e-mail messages attached to it are private and confidential. They may contain > proprietary or copyright material or information that is subject to legal > professional privilege. They are for the use of the intended recipient only. > Any unauthorised viewing, use, disclosure, copying, alteration, storage or > distribution of, or reliance on, this message is strictly prohibited. No part > may be reproduced, adapted or transmitted without the written permission of > the owner. If you have received this transmission in error, or are not an > authorised recipient, please immediately notify the sender by return email, > delete this message and all copies from your e-mail system, and destroy any > printed copies. Receipt by anyone other than the intended recipient should > not be deemed a waiver of any privilege or protection. Thales Australia does > not warrant or represent that this e-mail or any documents, files and > previous e-mail messages attached are error or virus free. > ------------------------------------------------------------------------- > > -- > 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. > ------------------------------------------------------------------------- > DISCLAIMER: This e-mail transmission and any documents, files and previous > e-mail messages attached to it are private and confidential. They may contain > proprietary or copyright material or information that is subject to legal > professional privilege. They are for the use of the intended recipient only. > Any unauthorised viewing, use, disclosure, copying, alteration, storage or > distribution of, or reliance on, this message is strictly prohibited. No part > may be reproduced, adapted or transmitted without the written permission of > the owner. If you have received this transmission in error, or are not an > authorised recipient, please immediately notify the sender by return email, > delete this message and all copies from your e-mail system, and destroy any > printed copies. Receipt by anyone other than the intended recipient should > not be deemed a waiver of any privilege or protection. Thales Australia does > not warrant or represent that this e-mail or any documents, files and > previous e-mail messages attached are error or virus free. > ------------------------------------------------------------------------- > > -- > 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.