On Oct 1, 2010, at 12:30 PM, Landreville wrote: > I need to make a call to an external system (through SOAP) when a new > object is saved/updated/deleted. If the external call fails then I > would like the save to also fail. I have looked at the documentation > and a MapperExtension cannot change the flush plan to cancel if the > external call fails. > > I have looked at session extensions and it looks like they would work, > but it seems far removed from my object. > I would have to check each object in the session to see if it is the > correct type and then execute the external method and then remove the > object from the session if it fails. Is this the recommended way of > binding an external action when modifying an object? > > I suppose I could add a before_insert method to my object and have a > session extension that just checks every object to see if it has > before_insert and if it does then execute it (and any errors would > remove the object from the session).
Any extension, mapper or session, can cancel the operation overall by raising an exception, which will result in halting the flush and issuing a ROLLBACK. Usually, if something goes "wrong" inside of a transaction, the whole thing is rolled back since you otherwise have only a partial state of data. Inside of mapper extension, a simplistic "dont update the row" operation may be possible if you just expired the attributes of the instance inside of before_update(), though I haven't tried this and it wouldnt really work for inserts or deletes anyway. Within session extension, its common to search for objects by traversing through the .new, .dirty and .deleted collections. 0.7 will be expanding on this model by providing event listener hooks that do this filtering for you, sending only objects of the desired type to the listener, and from that it follows that we can eventually optimize the search by improving the bookkeeping performed by these collections. SessionExtension is definitely the more generic choice overall these days for flush events. the use cases for MapperExtension are much smaller. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
