Skip to content

Commit

Permalink
Merge pull request #6 from metabase/use-get-connection
Browse files Browse the repository at this point in the history
Use DriverManager/getConnection() instead of DriverManager/getDriver()
  • Loading branch information
camsaul authored Dec 4, 2019
2 parents beea677 + 7070701 commit 68b9967
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject metabase/connection-pool "1.1.0"
(defproject metabase/connection-pool "1.1.1"
:description "Connection pools for JDBC databases. Simple wrapper around C3P0."
:url "https://github.com/metabase/connection-pool"
:min-lein-version "2.5.0"
Expand Down
22 changes: 15 additions & 7 deletions src/metabase/connection_pool.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@

;;; ------------------------------------------------ Proxy DataSource ------------------------------------------------

(defn- set-username-and-password! [^Properties properties username password]
(let [properties (or properties (Properties.))]
(doseq [[k v] {"user" username, "password" password}]
(if (some? v)
(.setProperty properties k (name v))
(.remove properties k)))
properties))

(defn- proxy-data-source
"Normal c3p0 DataSource classes do not properly work with our JDBC proxy drivers for whatever reason. Use our own
instead, which works nicely."
(^DataSource [^String jdbc-url, ^Properties properties]
(proxy-data-source (DriverManager/getDriver jdbc-url) jdbc-url properties))
(reify DataSource
(getConnection [_]
(DriverManager/getConnection jdbc-url properties))

(getConnection [_ username password]
(DriverManager/getConnection jdbc-url (set-username-and-password! properties username password)))))

(^DataSource [^Driver driver, ^String jdbc-url, ^Properties properties]
(reify DataSource
(getConnection [_]
(.connect driver jdbc-url properties))

(getConnection [_ username password]
(let [properties (or properties (Properties.))]
(doseq [[k v] {"user" username, "password" password}]
(if (some? v)
(.setProperty properties k (name v))
(.remove properties k)))
(.connect driver jdbc-url properties))))))
(.connect driver jdbc-url (set-username-and-password! properties username password))))))


;;; ------------------------------------------- Creating Connection Pools --------------------------------------------
Expand Down

0 comments on commit 68b9967

Please sign in to comment.