Browse files

Another attempt to fix JDBC-26 by incorporating the missing parts of

Nelson's patch.
  • Loading branch information...
1 parent 34ac3e5 commit 5818d002521fceef94065f962e7909fd9ae04feb @seancorfield seancorfield committed Feb 29, 2012
Showing with 24 additions and 5 deletions.
  1. +10 −5 src/main/clojure/clojure/java/jdbc/internal.clj
  2. +14 −0 src/test/clojure/clojure/java/test_jdbc.clj
View
15 src/main/clojure/clojure/java/jdbc/internal.clj
@@ -337,11 +337,16 @@
(with-open [^PreparedStatement stmt (prepare-statement* (connection*) sql :return-keys true)]
(set-parameters stmt param-group)
(transaction* (fn [] (let [counts (.executeUpdate stmt)]
- (try
- (first (resultset-seq* (.getGeneratedKeys stmt)))
- (catch Exception _
- ;; assume generated keys is unsupported and return counts instead:
- counts)))))))
+ (try
+ (let [rs (.getGeneratedKeys stmt)
+ result (first (resultset-seq* rs))]
+ ;; sqlite (and maybe others?) requires
+ ;; record set to be closed
+ (.close rs)
+ result)
+ (catch Exception _
+ ;; assume generated keys is unsupported and return counts instead:
+ counts)))))))
(defn do-prepared*
"Executes an (optionally parameterized) SQL prepared statement on the
View
14 src/test/clojure/clojure/java/test_jdbc.clj
@@ -327,6 +327,20 @@
(is (= 0 (sql/with-query-results res ["SELECT * FROM fruit"] (count res))))))
(is (= 0 (sql/with-query-results res ["SELECT * FROM fruit"] (count res)))))))
+(deftest test-transactions-with-possible-generated-keys-result-set
+ (doseq [db (test-specs)]
+ (sql/with-connection db
+ (create-test-table :fruit db)
+ (try
+ (sql/transaction
+ (sql/set-rollback-only)
+ (sql/insert-values
+ :fruit
+ [:name :appearance]
+ ["Grape" "yummy"])
+ (is (= 1 (sql/with-query-results res ["SELECT * FROM fruit"] (count res))))))
+ (is (= 0 (sql/with-query-results res ["SELECT * FROM fruit"] (count res)))))))
+
(deftest test-metadata
(doseq [db (test-specs)]
(when-not (.endsWith ^String (:subprotocol db) "sqlserver")

0 comments on commit 5818d00

Please sign in to comment.