c3p0, a mature, highly concurrent JDBC Connection pooling library for clojure.jdbc
Leiningen
[clojure.jdbc/clojure.jdbc-c3p0 "0.3.4"]
Gradle
compile "clojure.jdbc:clojure.jdbc-c3p0:0.3.4"
Maven
<dependency>
<groupId>clojure.jdbc</groupId>
<artifactId>clojure.jdbc-c3p0</artifactId>
<version>0.3.4</version>
</dependency>
In order to use a connection pool, you should convert your plain dbspec into a datasource-dbspec using the helper function provided in each extension.
;; Import the desired implementation
(require '[jdbc.pool.c3p0 :as pool])
;; Convert the standard dbspec to an other dbspec with `:datasource` key
(def dbspec (pool/make-datasource-spec {:subprotocol "postgresql"
:subname "//localhost:5432/dbname"}))
Now, dbspec should be used like a plain dbspec for creating connections.
c3p0 comes with "good" defaults that should work in majority standard environments, but obviously, it exposes set of options for customize it:
Option | Description |
---|---|
|
Minimum number of Connections a pool will maintain at any given time. |
|
Maximum number of Connections a pool will maintain at any given time. |
|
Number of Connections a pool will try to acquire upon startup. |
|
The number of milliseconds a client calling getConnection() will wait for a Connection to be checked-in or acquired when the pool is exhausted. |
|
The maximum lifetime in milliseconds of a connection. |
|
The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified, this query MUST be an SQL SELECT statement that returns at least one row. If not specified, connections will be validation by calling the isValid() method. |
|
The indication of whether objects will be validated before being borrowed from the pool. |
|
The indication of whether objects will be validated before being returned to the pool. |
|
The number of milliseconds to sleep between runs of the idle object evictor thread. |
|
The minimum amount of time (in milliseconds) an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any). |
Since clojure.jdbc 0.4.0-beta1, packages like this is not the recommended way to setup the connection pool. It provides the basic setup. If you want more control and access to all options, consider using the dbcp directly as documented in http://niwibe.github.io/clojure.jdbc/latest/#connection-pooloptions.