clojure – 如何撤销数据库中的事务?
发布时间:2020-12-12 08:42:54 所属栏目:MsSql教程 来源:网络整理
导读:我意外地对数据库进行了交易,我想“撤消”整个交易.我知道它是哪个事务,我可以看到它的数据,但我不知道如何从那里到一个回滚事务. 解决方法 基本步骤: 检索您要撤消的事务中创建的数据.使用事务日志找到它们. 删除与事务实体本身相关的数据:我们不想收回事
我意外地对数据库进行了交易,我想“撤消”整个交易.我知道它是哪个事务,我可以看到它的数据,但我不知道如何从那里到一个回滚事务.
解决方法基本步骤:>检索您要撤消的事务中创建的数据.使用事务日志找到它们. 在Clojure中,您的代码将如下所示: (defn rollback "Reassert retracted datoms and retract asserted datoms in a transaction,effectively "undoing" the transaction. WARNING: *very* naive function!" [conn tx] (let [tx-log (-> conn d/log (d/tx-range tx nil) first) ; find the transaction txid (-> tx-log :t d/t->tx) ; get the transaction entity id newdata (->> (:data tx-log) ; get the datoms from the transaction (remove #(= (:e %) txid)) ; remove transaction-metadata datoms ; invert the datoms add/retract state. (map #(do [(if (:added %) :db/retract :db/add) (:e %) (:a %) (:v %)])) reverse)] ; reverse order of inverted datoms. @(d/transact conn newdata))) ; commit new datoms. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |