From f0c4159bffbe9e62b7c34da0b421d399afe13a01 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Fri, 20 Mar 2020 13:51:50 -0700 Subject: [PATCH] Support loginTimeout on reified DataSource --- CHANGELOG.md | 1 + src/next/jdbc/connection.clj | 24 ++++++++++++++---------- test/next/jdbc/connection_test.clj | 4 ++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ae40fd..9f0d685 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: * 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 diff --git a/src/next/jdbc/connection.clj b/src/next/jdbc/connection.clj index e1465be..b8550a2 100644 --- a/src/next/jdbc/connection.clj +++ b/src/next/jdbc/connection.clj @@ -132,7 +132,8 @@ (defn- get-driver-connection "Common logic for loading the designated JDBC driver class and obtaining the appropriate `Connection` object." - [url etc] + [url timeout etc] + (when timeout (DriverManager/setLoginTimeout timeout)) (DriverManager/getConnection url (as-properties etc))) (def ^:private driver-cache @@ -238,15 +239,18 @@ "Given a JDBC URL and a map of options, return a `DataSource` that can be used to obtain a new database connection." [[url etc]] - (reify DataSource - (getConnection [_] - (get-driver-connection url etc)) - (getConnection [_ username password] - (get-driver-connection url - (assoc etc - :user username - :password password))) - (toString [_] url))) + (let [login-timeout (atom nil)] + (reify DataSource + (getConnection [_] + (get-driver-connection url @login-timeout etc)) + (getConnection [_ username password] + (get-driver-connection url @login-timeout + (assoc etc + :user username + :password password))) + (getLoginTimeout [_] (or @login-timeout 0)) + (setLoginTimeout [_ secs] (reset! login-timeout secs)) + (toString [_] url)))) (defn- make-connection "Given a `DataSource` and a map of options, get a connection and update it diff --git a/test/next/jdbc/connection_test.clj b/test/next/jdbc/connection_test.clj index 6fa438b..356026e 100644 --- a/test/next/jdbc/connection_test.clj +++ b/test/next/jdbc/connection_test.clj @@ -101,6 +101,8 @@ ds (p/get-datasource url)] (is (instance? javax.sql.DataSource ds)) (is (str/index-of (pr-str ds) url)) + (.setLoginTimeout ds 0) + (is (= 0 (.getLoginTimeout ds))) (with-open [con (p/get-connection ds {})] (is (instance? java.sql.Connection con))))) (testing "datasource via jdbcUrl" @@ -113,6 +115,8 @@ (is (str/index-of (pr-str ds) (str "jdbc:" (:dbtype db)))) ;; checks get-datasource on a DataSource is identity (is (identical? ds (p/get-datasource ds))) + (.setLoginTimeout ds 1) + (is (= 1 (.getLoginTimeout ds))) (with-open [con (p/get-connection ds {})] (is (instance? java.sql.Connection con))))) (testing "datasource via HikariCP"