Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added support for transaction retries to yesql
  • Loading branch information
ibarrick committed Sep 4, 2019
commit c9d4e3f0eac9b0e0b42ad1ee76bb2f1567445f4e
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(defproject org.batch/yesql "0.6.6"
(defproject org.batch/yesql "0.7.0"
:description "A Clojure library for using SQL"
:url "https://github.com/krisajenkins/yesql"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/java.jdbc "0.7.6"]
[instaparse "1.4.1" :exclusions [org.clojure/clojure]]]
[instaparse "1.4.1" :exclusions [org.clojure/clojure]]
[mysql/mysql-connector-java "5.1.44"]]
:pedantic? :abort
:scm {:name "git"
:url "https://github.com/krisajenkins/yesql"}
Expand Down
23 changes: 19 additions & 4 deletions src/yesql/generate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[yesql.types :refer [map->Query]]
[yesql.statement-parser :refer [tokenize]])
(:import [yesql.types Query])
(import java.lang.IllegalArgumentException))
(:import java.lang.IllegalArgumentException))

(def in-list-parameter?
"Check if a type triggers IN-list expansion."
Expand Down Expand Up @@ -138,9 +138,24 @@
((:before-delete query-options) args statement call-options)
(and (= execute-handler jdbc-fn) (= "update" (lower-case (first (split (trim statement) #" ")))) (:before-update query-options))
((:before-update query-options) args statement call-options))
(let [ret (jdbc-fn connection
(rewrite-query-for-jdbc tokens args)
call-options)]
(let [ret (jdbc/with-db-transaction [t connection]
(if (not= (:level t) 1)
(jdbc-fn connection
(rewrite-query-for-jdbc tokens args)
call-options)
(loop [i 0]
(Thread/sleep (* i 2 10))
(let [sym (try
(jdbc-fn connection
(rewrite-query-for-jdbc tokens args)
call-options)
(catch com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException e
(if (= i 10)
(throw e)
:exception-caught)))]
(if (= :exception-caught sym)
(recur (inc i))
sym)))))]
(cond (and (= insert-handler jdbc-fn) (:after-insert query-options))
((:after-insert query-options) ret args statement call-options)
(and (= execute-handler jdbc-fn) (= "delete" (lower-case (first (split (trim statement) #" ")))) (:after-delete query-options))
Expand Down