well yes, the way you're doing this is entirely the opposite of how the ORM is 
designed to function.    The Session has been developed in order to work in an 
intelligent manner with full graphs of interrelated objects, all coordinated 
under the umbrella of a transaction which applies atomicity and isolation to 
the full series of operations.    An individual object loaded from a Session is 
in fact an extension of that Session's state, which is in turn an extension of 
the state of the current transaction within the database, all kept in sync 
mostly automatically.   When you continuously break this connection between a 
whole series of interconnected objects you'll run into not just lots of 
problems keeping objects associated with continuously new transactions, but 
also horrendous performance from due to the excessive number of commits and 
re-acquisition of new connections/transactions.

In general, the Session is typically used in a "bounding" sense, its lifespan 
beginning before you work with any objects, and ending only after you've 
completed the work with those objects.    I frequently use the metaphor here 
that the Session is the table setting and the objects are the meal.   

Docs on why "commit" forces a reload by default and all the rest of this is 
explained pretty well at http://docs.sqlalchemy.org/en/rel_0_7/orm/session.html 
.



On May 30, 2012, at 4:21 AM, Maurizio Nagni wrote:

> Hello all,
> 
> my curious situation is the following. A very simplified version of the code 
> is:
> 
> for data in res:
>      obj = MyObject()
>      ---here I fill the obj, aventually doing some query (create session, 
> get, close) to SA----
> 
>      sess = createSession()
>      sess.add(obj)
>      sess.commit()
>      sess.close()
> 
>      -- do some other query (create session, get, close) and eventually 
> persist the changes (create session, merge/add, commit close)
> 
> now... on the first loop it works fine, then I receive the "Parent 
> instance...." warning me that a an obj inner attribute, say a contact, is not 
> bound so cannot load contact.phone attribute. BTW I receive such message  
> when I commit() but I am able to make it appear looking in the second loop, 
> during a debugging session, at the specific obj attribute.
> 
> What I would like to implement is to restrict the "session" (in a more 
> general sense SA activity even if I know that actually SA inject some 
> instrumentation in obj at the creation time) inside a unique class, say with 
> some static methods (the code above involving the session is in a separate 
> class) in order to centralize the operation toward the DB, and doing this it 
> should act in a "stateless" way (createSession, do stuff, commit/rollback, 
> close), unfortunately seems that I am missing something.
> 
> Thanks
> Maurizio
> 
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/sqlalchemy/-/J4THN4X57aUJ.
> 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