Skip to content

Commit c9d4e3f

Browse files
committed
added support for transaction retries to yesql
1 parent 5d95346 commit c9d4e3f

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

project.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
(defproject org.batch/yesql "0.6.6"
1+
(defproject org.batch/yesql "0.7.0"
22
:description "A Clojure library for using SQL"
33
:url "https://github.com/krisajenkins/yesql"
44
:license {:name "Eclipse Public License"
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
66
:dependencies [[org.clojure/clojure "1.8.0"]
77
[org.clojure/java.jdbc "0.7.6"]
8-
[instaparse "1.4.1" :exclusions [org.clojure/clojure]]]
8+
[instaparse "1.4.1" :exclusions [org.clojure/clojure]]
9+
[mysql/mysql-connector-java "5.1.44"]]
910
:pedantic? :abort
1011
:scm {:name "git"
1112
:url "https://github.com/krisajenkins/yesql"}

src/yesql/generate.clj

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[yesql.types :refer [map->Query]]
77
[yesql.statement-parser :refer [tokenize]])
88
(:import [yesql.types Query])
9-
(import java.lang.IllegalArgumentException))
9+
(:import java.lang.IllegalArgumentException))
1010

1111
(def in-list-parameter?
1212
"Check if a type triggers IN-list expansion."
@@ -138,9 +138,24 @@
138138
((:before-delete query-options) args statement call-options)
139139
(and (= execute-handler jdbc-fn) (= "update" (lower-case (first (split (trim statement) #" ")))) (:before-update query-options))
140140
((:before-update query-options) args statement call-options))
141-
(let [ret (jdbc-fn connection
142-
(rewrite-query-for-jdbc tokens args)
143-
call-options)]
141+
(let [ret (jdbc/with-db-transaction [t connection]
142+
(if (not= (:level t) 1)
143+
(jdbc-fn connection
144+
(rewrite-query-for-jdbc tokens args)
145+
call-options)
146+
(loop [i 0]
147+
(Thread/sleep (* i 2 10))
148+
(let [sym (try
149+
(jdbc-fn connection
150+
(rewrite-query-for-jdbc tokens args)
151+
call-options)
152+
(catch com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException e
153+
(if (= i 10)
154+
(throw e)
155+
:exception-caught)))]
156+
(if (= :exception-caught sym)
157+
(recur (inc i))
158+
sym)))))]
144159
(cond (and (= insert-handler jdbc-fn) (:after-insert query-options))
145160
((:after-insert query-options) ret args statement call-options)
146161
(and (= execute-handler jdbc-fn) (= "delete" (lower-case (first (split (trim statement) #" ")))) (:after-delete query-options))

0 commit comments

Comments
 (0)