Guest User

Untitled

a guest
Jul 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. (ns my.util.sql
  2. (:use clojure.contrib.condition)
  3. (:require
  4. [clojure.contrib.sql :as sql]
  5. [clj-record.core :as rec]
  6. [my.util.error :as err]))
  7.  
  8. (def *transaction-error* nil) ; While a transaction-wrapper is running, bound to the last error message set by rollback-with-raise
  9.  
  10. (defn rollback-with-raise
  11. "Call within a transaction body to rollback the transaction and raise a condition
  12. with the related error-code"
  13. [error-code]
  14. (println "ROLLING BACK: " error-code)
  15. (set! *transaction-error* error-code)
  16. (sql/set-rollback-only))
  17.  
  18. (defmacro transaction
  19. "Execute body in a clj-record transaction. rollback-with-raise may be called in body and will
  20. rollback the transaction and raise a condition with the specified error-code"
  21. [db-spec & body]
  22. `(binding [my.util.sql/*transaction-error* nil]
  23. (try
  24. (let [tx-result# (rec/transaction ~db-spec ~@body)]
  25. (if-not (nil? *transaction-error*)
  26. (err/raise-with-message *transaction-error*))
  27. tx-result#)
  28. (catch Exception e#
  29. (println "CAUGHT: " e#)
  30. (if-not (nil? *transaction-error*)
  31. (err/raise-with-message *transaction-error*)
  32. (err/raise-with-message :transaction-wrapper-error e#))))))
Add Comment
Please, Sign In to add comment