Support connection pooling more easily

This commit is contained in:
Sean Corfield 2019-07-17 23:50:56 -07:00
parent 5cadd9248b
commit 60c1e6660f
5 changed files with 32 additions and 4 deletions

View file

@ -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:
* 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

View file

@ -1,8 +1,13 @@
{: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
{:test {:extra-paths ["test"]
: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.hsqldb/hsqldb {:mvn/version "2.4.1"}
com.h2database/h2 {:mvn/version "1.4.197"}

View file

@ -2,7 +2,8 @@
(ns next.jdbc.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)
(javax.sql DataSource)
(java.util Properties)))
@ -191,6 +192,13 @@
(throw (ex-info (str "Unknown dbtype: " dbtype) db-spec)))
[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
"Given a JDBC URL, return it with an empty set of options with no parsing."
[s]

View file

@ -17,6 +17,7 @@
(:require [clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st]
[next.jdbc :as jdbc]
[next.jdbc.connection :as connection]
[next.jdbc.prepare :as prepare]
[next.jdbc.sql :as sql])
(:import (java.sql Connection PreparedStatement)
@ -113,6 +114,10 @@
:opts (s/? ::opts-map)))
:body (s/* any?)))
(s/fdef connection/jdbc-url
:args (s/cat :clazz #(instance? Class %)
:db-spec ::db-spec-map))
(s/fdef prepare/execute-batch!
:args (s/cat :ps ::prepared-statement
:param-groups (s/coll-of ::params :kind sequential?)
@ -187,6 +192,7 @@
`jdbc/execute-one!
`jdbc/transact
`jdbc/with-transaction
`connection/jdbc-url
`prepare/execute-batch!
`prepare/set-parameters
`sql/insert!

View file

@ -6,7 +6,8 @@
At some point, the datasource/connection tests should probably be extended
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]]
[next.jdbc.connection :as c]
[next.jdbc.protocols :as p]))
@ -103,4 +104,12 @@
(is (instance? java.sql.Connection con)))))
(testing "connection via map (Object)"
(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))))))