Support connection pooling more easily
This commit is contained in:
parent
5cadd9248b
commit
60c1e6660f
5 changed files with 32 additions and 4 deletions
|
|
@ -6,7 +6,7 @@ Only accretive/fixative changes will be made from now on.
|
||||||
|
|
||||||
The following changes have been committed to the **master** branch since the 1.0.2 release:
|
The following changes have been committed to the **master** branch since the 1.0.2 release:
|
||||||
|
|
||||||
* None.
|
* Address #48 by adding `next.jdbc.connection/jdbc-url` and showing HikariCP and c3p0 connection pooling (so far it's just tests).
|
||||||
|
|
||||||
## Stable Builds
|
## Stable Builds
|
||||||
|
|
||||||
|
|
|
||||||
7
deps.edn
7
deps.edn
|
|
@ -1,8 +1,13 @@
|
||||||
{:paths ["src"]
|
{:paths ["src"]
|
||||||
:deps {org.clojure/clojure {:mvn/version "1.10.1"}}
|
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
|
||||||
|
org.clojure/java.data {:mvn/version "0.1.1"}}
|
||||||
:aliases
|
:aliases
|
||||||
{:test {:extra-paths ["test"]
|
{:test {:extra-paths ["test"]
|
||||||
:extra-deps {org.clojure/test.check {:mvn/version "0.9.0"}
|
:extra-deps {org.clojure/test.check {:mvn/version "0.9.0"}
|
||||||
|
;; connection pooling
|
||||||
|
com.zaxxer/HikariCP {:mvn/version "3.3.1"}
|
||||||
|
com.mchange/c3p0 {:mvn/version "0.9.5.4"}
|
||||||
|
;; JDBC drivers
|
||||||
org.apache.derby/derby {:mvn/version "10.14.2.0"}
|
org.apache.derby/derby {:mvn/version "10.14.2.0"}
|
||||||
org.hsqldb/hsqldb {:mvn/version "2.4.1"}
|
org.hsqldb/hsqldb {:mvn/version "2.4.1"}
|
||||||
com.h2database/h2 {:mvn/version "1.4.197"}
|
com.h2database/h2 {:mvn/version "1.4.197"}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
(ns next.jdbc.connection
|
(ns next.jdbc.connection
|
||||||
"Standard implementations of `get-datasource` and `get-connection`."
|
"Standard implementations of `get-datasource` and `get-connection`."
|
||||||
(:require [next.jdbc.protocols :as p])
|
(:require [clojure.java.data :refer [to-java]]
|
||||||
|
[next.jdbc.protocols :as p])
|
||||||
(:import (java.sql Connection DriverManager)
|
(:import (java.sql Connection DriverManager)
|
||||||
(javax.sql DataSource)
|
(javax.sql DataSource)
|
||||||
(java.util Properties)))
|
(java.util Properties)))
|
||||||
|
|
@ -191,6 +192,13 @@
|
||||||
(throw (ex-info (str "Unknown dbtype: " dbtype) db-spec)))
|
(throw (ex-info (str "Unknown dbtype: " dbtype) db-spec)))
|
||||||
[url etc]))
|
[url etc]))
|
||||||
|
|
||||||
|
(defn jdbc-url
|
||||||
|
"Given a connection pooling class and a database spec, return an connection
|
||||||
|
pool object built from the database spec."
|
||||||
|
[clazz db-spec]
|
||||||
|
(let [[url etc] (spec->url+etc db-spec)]
|
||||||
|
(to-java clazz (assoc etc :jdbcUrl url))))
|
||||||
|
|
||||||
(defn- string->url+etc
|
(defn- string->url+etc
|
||||||
"Given a JDBC URL, return it with an empty set of options with no parsing."
|
"Given a JDBC URL, return it with an empty set of options with no parsing."
|
||||||
[s]
|
[s]
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
(:require [clojure.spec.alpha :as s]
|
(:require [clojure.spec.alpha :as s]
|
||||||
[clojure.spec.test.alpha :as st]
|
[clojure.spec.test.alpha :as st]
|
||||||
[next.jdbc :as jdbc]
|
[next.jdbc :as jdbc]
|
||||||
|
[next.jdbc.connection :as connection]
|
||||||
[next.jdbc.prepare :as prepare]
|
[next.jdbc.prepare :as prepare]
|
||||||
[next.jdbc.sql :as sql])
|
[next.jdbc.sql :as sql])
|
||||||
(:import (java.sql Connection PreparedStatement)
|
(:import (java.sql Connection PreparedStatement)
|
||||||
|
|
@ -113,6 +114,10 @@
|
||||||
:opts (s/? ::opts-map)))
|
:opts (s/? ::opts-map)))
|
||||||
:body (s/* any?)))
|
:body (s/* any?)))
|
||||||
|
|
||||||
|
(s/fdef connection/jdbc-url
|
||||||
|
:args (s/cat :clazz #(instance? Class %)
|
||||||
|
:db-spec ::db-spec-map))
|
||||||
|
|
||||||
(s/fdef prepare/execute-batch!
|
(s/fdef prepare/execute-batch!
|
||||||
:args (s/cat :ps ::prepared-statement
|
:args (s/cat :ps ::prepared-statement
|
||||||
:param-groups (s/coll-of ::params :kind sequential?)
|
:param-groups (s/coll-of ::params :kind sequential?)
|
||||||
|
|
@ -187,6 +192,7 @@
|
||||||
`jdbc/execute-one!
|
`jdbc/execute-one!
|
||||||
`jdbc/transact
|
`jdbc/transact
|
||||||
`jdbc/with-transaction
|
`jdbc/with-transaction
|
||||||
|
`connection/jdbc-url
|
||||||
`prepare/execute-batch!
|
`prepare/execute-batch!
|
||||||
`prepare/set-parameters
|
`prepare/set-parameters
|
||||||
`sql/insert!
|
`sql/insert!
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@
|
||||||
|
|
||||||
At some point, the datasource/connection tests should probably be extended
|
At some point, the datasource/connection tests should probably be extended
|
||||||
to accept EDN specs from an external source (environment variables?)."
|
to accept EDN specs from an external source (environment variables?)."
|
||||||
(:require [clojure.string :as str]
|
(:require [clojure.java.data :refer [to-java]]
|
||||||
|
[clojure.string :as str]
|
||||||
[clojure.test :refer [deftest is testing]]
|
[clojure.test :refer [deftest is testing]]
|
||||||
[next.jdbc.connection :as c]
|
[next.jdbc.connection :as c]
|
||||||
[next.jdbc.protocols :as p]))
|
[next.jdbc.protocols :as p]))
|
||||||
|
|
@ -103,4 +104,12 @@
|
||||||
(is (instance? java.sql.Connection con)))))
|
(is (instance? java.sql.Connection con)))))
|
||||||
(testing "connection via map (Object)"
|
(testing "connection via map (Object)"
|
||||||
(with-open [con (p/get-connection db {})]
|
(with-open [con (p/get-connection db {})]
|
||||||
|
(is (instance? java.sql.Connection con))))
|
||||||
|
(testing "connection via HikariCP"
|
||||||
|
(with-open [con (p/get-connection (c/jdbc-url com.zaxxer.hikari.HikariDataSource db)
|
||||||
|
{})]
|
||||||
|
(is (instance? java.sql.Connection con))))
|
||||||
|
(testing "connection via c3p0"
|
||||||
|
(with-open [con (p/get-connection (c/jdbc-url com.mchange.v2.c3p0.ComboPooledDataSource db)
|
||||||
|
{})]
|
||||||
(is (instance? java.sql.Connection con))))))
|
(is (instance? java.sql.Connection con))))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue