Skip to content

Commit

Permalink
Unwrap RTE in nested transaction exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
seancorfield committed Apr 15, 2012
1 parent 7b706ed commit c498521
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
@@ -1,3 +1,8 @@
Changes in 0.1.4:

* Unwrap RTE for nested transaction exception (we already unwrapped top-level transaction RTEs).
* Remove reflection warning unwrapping RunTimeException (Alan Malloy)

Changes in 0.1.3:

* Fix JDBC-26 (fully) by adding transaction/generated keys support for SQLite3 (based on patch from Nelson Morris)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -88,6 +88,10 @@ Developer Information
Change Log
====================

* Release 0.1.4 on ????
* Unwrap RTE for nested transaction exceptions (we already
unwrapped top-level transaction RTEs).
* Remove reflection warning unwrapping RunTimeException (Alan Malloy)
* Release 0.1.3 on 2012-02-29
* Fix generated keys inside transactions for SQLite3 [JDBC-26](http://dev.clojure.org/jira/browse/JDBC-26)
* Release 0.1.2 on 2012-02-29
Expand Down
51 changes: 27 additions & 24 deletions src/main/clojure/clojure/java/jdbc/internal.clj
Expand Up @@ -197,32 +197,35 @@
complete."
[func]
(binding [*db* (update-in *db* [:level] inc)]
(if (= (:level *db*) 1)
(let [^Connection con (connection*)
auto-commit (.getAutoCommit con)]
(io!
(.setAutoCommit con false)
(try
(let [result (func)]
(if (rollback)
(.rollback con)
(.commit con))
result)
(catch Exception e
(.rollback con)
;; This ugliness makes it easier to catch SQLException objects
;; rather than something wrapped in a RuntimeException which
;; can really obscure your code when working with JDBC from
;; Clojure... :(
(letfn [(throw-non-rte [^Throwable ex]
(cond (instance? java.sql.SQLException ex) (throw ex)
(and (instance? RuntimeException ex) (.getCause ex)) (throw-non-rte (.getCause ex))
:else (throw ex)))]
(throw-non-rte e)))
(finally
;; This ugliness makes it easier to catch SQLException objects
;; rather than something wrapped in a RuntimeException which
;; can really obscure your code when working with JDBC from
;; Clojure... :(
(letfn [(throw-non-rte [^Throwable ex]
(cond (instance? java.sql.SQLException ex) (throw ex)
(and (instance? RuntimeException ex) (.getCause ex)) (throw-non-rte (.getCause ex))
:else (throw ex)))]
(if (= (:level *db*) 1)
(let [^Connection con (connection*)
auto-commit (.getAutoCommit con)]
(io!
(.setAutoCommit con false)
(try
(let [result (func)]
(if (rollback)
(.rollback con)
(.commit con))
result)
(catch Exception e
(.rollback con)
(throw-non-rte e))
(finally
(rollback false)
(.setAutoCommit con auto-commit)))))
(func))))
(try
(func)
(catch Exception e
(throw-non-rte e)))))))

(defn- make-name-unique
"Given a collection of column names and a new column name,
Expand Down

0 comments on commit c498521

Please sign in to comment.