using transactions FTW

This commit is contained in:
Bryce Covert
2018-05-14 10:40:57 -07:00
parent cffff99014
commit 431645ad42
3 changed files with 27 additions and 13 deletions

View File

@@ -7,9 +7,11 @@
[auto-ap.routes.graphql :as graphql]
[auto-ap.routes.vendors :as vendors]
[auto-ap.routes.checks :as checks]
[auto-ap.db.utils :as u]
[buddy.auth.backends.token :refer [jws-backend]]
[buddy.auth.middleware :refer [wrap-authentication
wrap-authorization]]
[clojure.java.jdbc :as jdbc]
[compojure.core :refer :all]
[compojure.route :as route]
[config.core :refer [env]]
@@ -44,8 +46,19 @@
api-routes
static-routes))
(defn wrap-transaction [handler]
(fn [request]
(jdbc/with-db-transaction [t (u/get-conn)]
(binding [u/current-conn t]
(try
(handler (assoc request :db-conn t))
(catch Exception e
(jdbc/db-set-rollback-only! t)
(throw e)))))))
(def app
(-> #'app-routes
(wrap-transaction)
(wrap-authorization auth-backend)
(wrap-authentication auth-backend)
(wrap-reload)