El Tea schrieb: > Let me preface this by saying I may still not having a sufficient > grasp of SQL Alchemy. > > I am adding a set of entries to the database, one at a time. Before > adding each entry I check to ensure a similar entry is not in the > database. If there is, I consider the entire set invalid and attempt > to do a rollback. So, in pseudocode: > > Loop through my input data: > Query DB to check for duplicate > if not duplicate: > DBSession.add(entry) > if duplicate: > DBSession.rollback() > return #Returns to the controller method, which then returns > result > data for rendering. > > I keep getting the following error: > > InvalidRequestError: The transaction is closed
Transactions in TG2 are managed through the repoze.tm2 middleware, *not* by you. Unless of course you rip that out, as we did. But that's another story. However, in any case the above code is faulty. You don't rollback a transaction when you'd have a duplicate entry, because then you'd rollback *all* the previously successfull creations. That's the way transactions work. Instead, you do nothing. And if you want to make 100% sure you don't run into a race-condition, you first lock the table you are working with, because otherwise it can happen that between checking for a row, and creating it, it is created by another request. Diez --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "TurboGears" 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/turbogears?hl=en -~----------~----~----~----~------~----~------~--~---

