Guest User

Untitled

a guest
Oct 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. (defn begin
  2. "Begin, invoke, commit, rollback if error"
  3. [func]
  4. (binding [clojure.java.jdbc/xa-transaction* (fn [f] (f))]
  5. (.begin manager)
  6. (try
  7. (let [result (func)]
  8. (.commit manager)
  9. result)
  10. (catch Throwable e
  11. (.rollback manager)
  12. (throw e)))))
  13.  
  14. (defn suspend
  15. "Suspend, invoke, resume"
  16. [func]
  17. (let [tx (.suspend manager)]
  18. (try
  19. (func)
  20. (finally
  21. (.resume manager tx)))))
  22.  
  23. (defmacro required
  24. "JEE Required"
  25. [& body]
  26. (let [f `(fn [] ~@body)]
  27. `(if (active?)
  28. (~f)
  29. (begin ~f))))
  30.  
  31. (defmacro requires-new
  32. "JEE RequiresNew"
  33. [& body]
  34. (let [f `(fn [] ~@body)]
  35. `(if (active?)
  36. (suspend #(begin ~f))
  37. (begin ~f))))
  38.  
  39. (defmacro mandatory
  40. "JEE Mandatory"
  41. [& body]
  42. (let [f `(fn [] ~@body)]
  43. `(if (active?)
  44. (~f)
  45. (throw (Exception. "No active transaction")))))
  46.  
  47. (defmacro not-supported
  48. "JEE NotSupported"
  49. [& body]
  50. (let [f `(fn [] ~@body)]
  51. `(if (active?)
  52. (suspend ~f)
  53. (~f))))
  54.  
  55. (defmacro supports
  56. "JEE Supports (kinda silly)"
  57. [& body]
  58. (let [f `(fn [] ~@body)]
  59. `(~f)))
  60.  
  61. (defmacro never
  62. "JEE Never"
  63. [& body]
  64. (let [f `(fn [] ~@body)]
  65. `(if (active?)
  66. (throw (Exception. "Active transaction detected"))
  67. (~f))))
Add Comment
Please, Sign In to add comment