Hao Yan <h...@uber.com> wrote:

> Want to ask you guys opinions about this :In the spirit of separating 
> lifecycle of sqlalchemy session from actual operations on db, sqlalchemy 
> suggests the following pattern 
> (http://docs.sqlalchemy.org/en/rel_0_9/orm/session_basics.html ):
> 
> 
>     def go(self, session):
>           session.query(Widget).all()
> The point is: if the operation is Query only (not write), should we also wrap 
> it with the session scope. My understanding is: a transaction is started even 
> if it is just a Query, and if it fails, the state of identityMap maintained 
> by session will be changed even if database is still unchanged, therefore, we 
> probably still need session.rollback() even if it is just a Query, since that 
> will also bring the identitymap state back to the normal state before the 
> Query. 


I’d agree. Your whole program should be constructed that there’s an
outermost fixture that all database operations run within. Read-only
operations are in transactions as well, so yes, I’d treat them all equally.


> However, i am not sure about this. The downside of this is, if this is not 
> necessary, then we introduce unnecessary wrapping around Query.
> BTW, there is another example 
> at:http://docs.sqlalchemy.org/en/rel_0_9/orm/session_transaction.html#session-external-transaction

A program that’s doing something non-trivial is going to have only one point
in the whole thing where there’s some outermost function call. Like a
main(), or a run(), or an application(). All other functions are going to be
called by that one very first function. So everything is “wrapped” anyway,
you just need to organize it such that the scope of database operations is
handled within that structure.

>     
> raise
> in the above example, they put "try" before session.query(Item).get(), 
> instead of right before "item1.foo='bar'), this kind of implies that query 
> could also need to be rollbacked when it fails. However, they did not 
> explicitly say that. 
> Basically, my question is what is the best practice for query only.

the session.query().get() operation starts up a transaction on a new
connection, so everything that’s in the transaction is what gets rolled back
if something goes wrong.

Just consider any kind of “conversation with the database” the same as “a
transaction”. Doesn’t matter if its for information only. You can go to your
ATM machine and only ask for your balance, that’s still all under the realm
of “transaction”.



> Please help! thanks a lot!
> 
> Hao Yan
> 
> -- 
> 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 http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to