always call setReadOnly when opening a transaction:

if the connection is read-only and the user supplies `{:read-only false}` as tx opts, we should call `(.setReadOnly conn false)` when we open the transaction, and reset it afterwards
This commit is contained in:
James Henderson 2022-09-06 16:47:57 +01:00
parent 65b26a0fa4
commit 61471aae22
No known key found for this signature in database
GPG key ID: 956F20CA2105F582
3 changed files with 15 additions and 4 deletions

View file

@ -65,8 +65,8 @@
(io!
(when isolation
(.setTransactionIsolation con (isolation isolation-levels)))
(when read-only
(.setReadOnly con true))
(when (contains? opts :read-only)
(.setReadOnly con (boolean read-only)))
(.setAutoCommit con false)
(try
(let [result (f con)]
@ -107,7 +107,7 @@
(try
(.setTransactionIsolation con old-isolation)
(catch Exception _)))
(when read-only
(when (contains? opts :read-only)
(try
(.setReadOnly con old-readonly)
(catch Exception _))))))))

View file

@ -90,6 +90,8 @@
(defn stored-proc? [] (not (#{"derby" "h2" "h2:mem" "sqlite"} (:dbtype @test-db-spec))))
(defn can-set-read-only? [] (not (#{"h2" "h2:mem" "sqlite"} (:dbtype @test-db-spec))))
(defn column [k]
(let [n (namespace k)]
(keyword (when n (cond (postgres?) (str/lower-case n)

View file

@ -9,7 +9,7 @@
[next.jdbc.connection :as c]
[next.jdbc.test-fixtures
:refer [with-test-db db ds column
default-options stored-proc?
default-options stored-proc? can-set-read-only?
derby? hsqldb? jtds? mssql? mysql? postgres? sqlite?]]
[next.jdbc.prepare :as prep]
[next.jdbc.result-set :as rs]
@ -821,6 +821,15 @@ INSERT INTO fruit (name, appearance) VALUES (?,?)
(with-open [con (jdbc/get-connection ds {})]
(is (instance? java.sql.Connection con)))))))
(deftest always-call-set-read-only-when-opening-a-tx
(when (can-set-read-only?)
(with-open [con (jdbc/get-connection (ds))]
(.setReadOnly con true)
(jdbc/with-transaction [tx con {:read-only false}]
(is (not (.isReadOnly con)))
(is (not (.isReadOnly tx))))
(is (.isReadOnly con)))))
(deftest multi-rs
(when (mssql?)
(testing "script with multiple result sets"