Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns my.util.sql
- (:use clojure.contrib.condition)
- (:require
- [clojure.contrib.sql :as sql]
- [clj-record.core :as rec]
- [my.util.error :as err]))
- (def *transaction-error* nil) ; While a transaction-wrapper is running, bound to the last error message set by rollback-with-raise
- (defn rollback-with-raise
- "Call within a transaction body to rollback the transaction and raise a condition
- with the related error-code"
- [error-code]
- (println "ROLLING BACK: " error-code)
- (set! *transaction-error* error-code)
- (sql/set-rollback-only))
- (defmacro transaction
- "Execute body in a clj-record transaction. rollback-with-raise may be called in body and will
- rollback the transaction and raise a condition with the specified error-code"
- [db-spec & body]
- `(binding [my.util.sql/*transaction-error* nil]
- (try
- (let [tx-result# (rec/transaction ~db-spec ~@body)]
- (if-not (nil? *transaction-error*)
- (err/raise-with-message *transaction-error*))
- tx-result#)
- (catch Exception e#
- (println "CAUGHT: " e#)
- (if-not (nil? *transaction-error*)
- (err/raise-with-message *transaction-error*)
- (err/raise-with-message :transaction-wrapper-error e#))))))
Add Comment
Please, Sign In to add comment