Support loginTimeout on reified DataSource

This commit is contained in:
Sean Corfield 2020-03-20 13:51:50 -07:00
parent eda9c93a1c
commit f0c4159bff
3 changed files with 19 additions and 10 deletions

View file

@ -7,6 +7,7 @@ Only accretive/fixative changes will be made from now on.
The following changes have been made to **master** since the 1.0.409 build: The following changes have been made to **master** since the 1.0.409 build:
* Fixes #101 by tightening the spec on a JDBC URL to correctly reflect that it must start with `jdbc:`. * Fixes #101 by tightening the spec on a JDBC URL to correctly reflect that it must start with `jdbc:`.
* Add support for calling `.getLoginTimeout`/`.setLoginTimeout` on the reified `DataSource` returned by `get-datasource` when called on a hash map "db-spec" or JDBC URL string.
## Stable Builds ## Stable Builds

View file

@ -132,7 +132,8 @@
(defn- get-driver-connection (defn- get-driver-connection
"Common logic for loading the designated JDBC driver class and "Common logic for loading the designated JDBC driver class and
obtaining the appropriate `Connection` object." obtaining the appropriate `Connection` object."
[url etc] [url timeout etc]
(when timeout (DriverManager/setLoginTimeout timeout))
(DriverManager/getConnection url (as-properties etc))) (DriverManager/getConnection url (as-properties etc)))
(def ^:private driver-cache (def ^:private driver-cache
@ -238,15 +239,18 @@
"Given a JDBC URL and a map of options, return a `DataSource` that can be "Given a JDBC URL and a map of options, return a `DataSource` that can be
used to obtain a new database connection." used to obtain a new database connection."
[[url etc]] [[url etc]]
(reify DataSource (let [login-timeout (atom nil)]
(getConnection [_] (reify DataSource
(get-driver-connection url etc)) (getConnection [_]
(getConnection [_ username password] (get-driver-connection url @login-timeout etc))
(get-driver-connection url (getConnection [_ username password]
(assoc etc (get-driver-connection url @login-timeout
:user username (assoc etc
:password password))) :user username
(toString [_] url))) :password password)))
(getLoginTimeout [_] (or @login-timeout 0))
(setLoginTimeout [_ secs] (reset! login-timeout secs))
(toString [_] url))))
(defn- make-connection (defn- make-connection
"Given a `DataSource` and a map of options, get a connection and update it "Given a `DataSource` and a map of options, get a connection and update it

View file

@ -101,6 +101,8 @@
ds (p/get-datasource url)] ds (p/get-datasource url)]
(is (instance? javax.sql.DataSource ds)) (is (instance? javax.sql.DataSource ds))
(is (str/index-of (pr-str ds) url)) (is (str/index-of (pr-str ds) url))
(.setLoginTimeout ds 0)
(is (= 0 (.getLoginTimeout ds)))
(with-open [con (p/get-connection ds {})] (with-open [con (p/get-connection ds {})]
(is (instance? java.sql.Connection con))))) (is (instance? java.sql.Connection con)))))
(testing "datasource via jdbcUrl" (testing "datasource via jdbcUrl"
@ -113,6 +115,8 @@
(is (str/index-of (pr-str ds) (str "jdbc:" (:dbtype db)))) (is (str/index-of (pr-str ds) (str "jdbc:" (:dbtype db))))
;; checks get-datasource on a DataSource is identity ;; checks get-datasource on a DataSource is identity
(is (identical? ds (p/get-datasource ds))) (is (identical? ds (p/get-datasource ds)))
(.setLoginTimeout ds 1)
(is (= 1 (.getLoginTimeout ds)))
(with-open [con (p/get-connection ds {})] (with-open [con (p/get-connection ds {})]
(is (instance? java.sql.Connection con))))) (is (instance? java.sql.Connection con)))))
(testing "datasource via HikariCP" (testing "datasource via HikariCP"