Hello world,

I am tearing my hair about my SQLAlchemy usage in a GUI application,
especially to get it error tolerant.

Usually the GUI filters user inputs before they are thrown at SQLAlchemy
to store it into the database. If that fails, however, it can happen
that data is thrown at the database that leads to a rollback because of
e.g. violated foreign key constraints.

If that happens, the session rolls back (fine) but the GUI still shows
the state that I tried to write into the database. Being MVC, I would
need all database object to fire a "changed" event so they are pulled
fresh from the database.

I tried using the after_commit extension (SQLAlchemy 0.6.8) to do this.
Paraphrased, this works like this:

    def after_rollback(self, session):
        for instance in session:
            instance.signal_changed()

This works fine if the transaction being rolled back is the toplevel
transaction. However, if that is a nested transaction, this destroys my
session state (as the listeners try to load from the database):

InvalidRequestError: This Session's transaction has been rolled back by
a nested rollback() call.  To begin a new transaction, issue
Session.rollback() first.

So I would need the callback only after the rollback of the toplevel
session occurred. I tried looking at the _is_transaction_boundary
property of session.transaction:

    def after_rollback(self, session):
        if session.transaction._is_transaction_boundary:
            for instance in session:
                instance.signal_changed()

This stops the exceptions, but it also never comes to signal_changed if
subtransactions are rolled back. It looks like I only get a single event
for the innermost transaction?!


What would be a sane way to implement this? I also looked at the
MapperExtension in the hope that there is a callback whenever an
instance is expired, but does not seem to be such a thing.

Hints welcome,

Torsten

-- 
DYNAmore Gesellschaft fuer Ingenieurdienstleistungen mbH
Torsten Landschoff

Office Dresden
Tel: +49-(0)351-4519587
Fax: +49-(0)351-4519561

mailto:torsten.landsch...@dynamore.de
http://www.dynamore.de

Registration court: Mannheim, HRB: 109659, based in Karlsruhe,
Managing director:  Prof. Dr. K. Schweizerhof, Dipl.-Math. U. Franz

-- 
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