回滚clj记录事务

时间:2010-11-25 23:49:02

标签: database transactions clojure

我正在使用clj-record / transaction两个包装两个记录创建。出错,我正在使用clojure.contrib.condition / raise来抛出异常,但这不会回滚任何东西。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我需要使用clojure.contrib.sql / set-rollback-only:

(ns my.namespace.txtest
  (:use clojure.contrib.condition)
  (:require clj-record.boot
    [clj-record.callbacks.built-ins     :as cb]
    [clj-record.core                    :as rec]
    [clj-record.validation.built-ins    :as valid]
    [clojure.contrib.sql                :as sql]))

(def db {:classname "com.mysql.jdbc.Driver" 
    :subprotocol "mysql" 
    :user "mydb" 
    :password "password" 
    :subname "//localhost/mydb"})

(defn do-something
  (rec/transaction db
    (do-something)
    (if (some-error-test)
      (do
        (sql/set-rollback-only)
        (raise :type "some-error" :message "Oops")))))