From 7c0cee09bf4a992216590f9ecd50b3a975f042f8 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Thu, 4 Jun 2020 22:30:38 -0700 Subject: [PATCH] Issue #117 sketch of dynamic nested transaction behavior --- src/next/jdbc/transaction.clj | 36 ++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/next/jdbc/transaction.clj b/src/next/jdbc/transaction.clj index e538149..a6534f9 100644 --- a/src/next/jdbc/transaction.clj +++ b/src/next/jdbc/transaction.clj @@ -79,15 +79,41 @@ (.setReadOnly con old-readonly) (catch Exception _)))))))) +(defonce ^:dynamic ^{:doc ""} *nested-tx* :allow) +(defonce ^:private ^:dynamic ^{:doc ""} *active-tx* false) + (extend-protocol p/Transactable java.sql.Connection (-transact [this body-fn opts] - (locking this - (transact* this body-fn opts))) + (cond (or (not *active-tx*) (= :allow *nested-tx*)) + (locking this + (binding [*active-tx* true] + (transact* this body-fn opts))) + (= :ignore *nested-tx*) + (body-fn this) + (= :prohibit *nested-tx*) + (throw (IllegalStateException. "Nested transactions are prohibited")) + :else + (throw (IllegalArgumentException. + (str "*nested-tx* (" + *nested-tx* + ") was not :allow, :ignore, or :prohibit"))))) javax.sql.DataSource (-transact [this body-fn opts] - (with-open [con (p/get-connection this opts)] - (transact* con body-fn opts))) + (cond (or (not *active-tx*) (= :allow *nested-tx*)) + (binding [*active-tx* true] + (with-open [con (p/get-connection this opts)] + (transact* con body-fn opts))) + (= :ignore *nested-tx*) + (with-open [con (p/get-connection this opts)] + (body-fn con)) + (= :prohibit *nested-tx*) + (throw (IllegalStateException. "Nested transactions are prohibited")) + :else + (throw (IllegalArgumentException. + (str "*nested-tx* (" + *nested-tx* + ") was not :allow, :ignore, or :prohibit"))))) Object (-transact [this body-fn opts] - (p/-transact (p/get-datasource this) body-fn opts))) + (p/-transact (p/get-datasource this) body-fn opts)))