На 16.07.2014 19:17 "Michael Bayer" <mike...@zzzcomputing.com> написа:
>
>
> On Jul 16, 2014, at 11:02 AM, Vladimir Iliev <vladimir.il...@gmail.com>
wrote:
>
> > hi,
> >
> > we're developing a desktop app using sa so the sessions is long living.
> > we use postgres and sqlite.
>
> if the desktop app is "dormant" between operations transaction-wise, you
might not need to have a "long living" session pattern.  It may be feasible
to open up a Session only when interaction with the database is required,
within the scope of a transaction.

that would over-complicate the code


>
> >
> > 1. sometimes we see same object twice in one to many relation.
> > it's very hard to reproduce and the problem is gone after commit.
> > does anyone else seen such a behavior?
>
> if you append the same object twice to a collection, it will be there
twice.  When it flushes, relationally this is impossible so only one row
actually gets updated; if the collection is then expired and reloaded (as
happens on commit()), you'll see just one.      I'd recommend using set()
for the collection class rather than list.

i'm pretty sure i'm not appending anything to collection but i'm updating
the other side, so the objects are auto-added to the collection..


>
> >
> > 2. sometimes i do "ob.relation = []" and repopulate with new entries
but after commit the old entries are still there.
> > i can't reproduce it in tests. it only happens while people use the app.
> > the relation is configured with cascade='all,delete-orphan'
>
> that would only occur if the change to ob.relation is expired before it
is flushed.   if some function that occurs within this repopulate happens
to call session.commit(), that is one potential way that could happen.
the points at which the application calls commit() should be well defined
and not arbitrary.

it's strange because there are no commits during repopulate. all the code
is wrapped in single transaction block.

>
> >
> > 3. all the write operations are wrapped in "with session.begin()"
blocks but sometimes unhandled error occures (mostly due bugs) and and the
session is left in "zombie mode".
> > is there a way to auto rollback last transaction in that case?
>
> with session.begin() will unconditionally emit a rollback() when an
exception is propagated outwards, unless the Session itself no longer has a
transaction present, in which case this rollback is not possible.  if your
application is failing in these blocks, it's very difficult to guarantee
that the app can continue running in all cases as the scope of the failure
can't be anticipated.   A catastrophic failure should probably not be
attempting to do anything with the same Session, as it still has state
within it that still could be related to the error.  There's no way to
know, if we're talking about failures with an undefined behavior.
>
>
> >
> > 4. what's the right way to modify session state in after
insert,update,delete event handler or something similar?
> > for example i have parent object with collection of child objects. i
want to recompute in python a column on the parent object whenever child is
added,updated or deleted.
> > in most cases triggers are fine but debugging sqlite triggers is
driving me nuts...
>
> the safest place to calculate changes to related objects in a flush is in
the session.before_flush() event.   Within the mapper/row-level hooks, it
is not safe to interact with related objects, as they may have already been
flushed, or are not part of the flush plan.

got it. thanks!


>
> --
> You received this message because you are subscribed to a topic in the
Google Groups "sqlalchemy" group.
> To unsubscribe from this topic, visit
https://groups.google.com/d/topic/sqlalchemy/vulI27CLD30/unsubscribe.
> To unsubscribe from this group and all its topics, 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