clojure – 如何撤销数据库中的事务?

我意外地对数据库进行了交易,我想“撤消”整个交易.我知道它是哪个事务,我可以看到它的数据,但我不知道如何从那里到一个回滚事务.

解决方法

基本步骤:

>检索您要撤消的事务中创建的数据.使用事务日志找到它们.
>删除与事务实体本身相关的数据:我们不想收回事务元数据.
>反转所有剩余数据的“添加”状态,即如果添加了数据,则将其撤回,如果已被撤销,则将其添加.
>反转倒数据的顺序,以便在重新确定旧的好值之前缩小坏的新值.
>提交一个新的事务.

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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...