On Monday, March 24, 2014 7:09:27 PM UTC-7, Tim Uckun wrote: > > I have an odd use case. I take data, process it and then put it into two > different databases the flow goes something like this > > db1=.... > db2= .... > > r1 = db1[sql].returning.insert > r2 = db2[sql].returning.insert > > The first thing odd about this is that r1 and r2 are both integers, I was > expecting an array. No big deal I guess but now I want to check to see if > both operations succeed. If one doesn't I want to roll back both of them. > So something like this >
If you are using returning, you probably want to use all or map. > > db1['start transaction'] > db2['start transaction'] > > r1 = db1[sql].returning.insert > r2 = db2[sql].returning.insert > > if (do some checking here) > db1['commit'] > db2['commit'] > else > db1[rollback'] > db2['rollback'] > end > > would this work as I think it should? Is there a better way? > You shouldn't do that. To correctly handle transactions in distributed databases, you should use prepared transactions/two phase commit, which Sequel supports on some databases. See http://sequel.jeremyevans.net/rdoc/files/doc/transactions_rdoc.html If you can't use prepared transactions, you can use Sequel.transaction: Sequel.transaction([db1, db2]) do r1 = db1[sql].returning.all r2 = db2[sql].returning.all raise Sequel::Rollback unless (do some checking here) end Note that Sequel.transaction uses regular transactions, so if the first database commits and the second database fails to commit (due to deferred constraint violations, for example), you have some serious problems. In case you are wondering, Sequel does not support a non-block form of transactions, since such an API isn't safe. Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
