Liking cljdoc? Tell your friends :D

clojure.jdbc-hikari

Fast, simple, reliable. HikariCP is a "zero-overhead" production ready JDBC connection pool implementation for clojure.jdbc.

Install

Leiningen

[clojure.jdbc/clojure.jdbc-hikari "0.3.3"]

Gradle

compile "clojure.jdbc:clojure.jdbc-hikari:0.3.3"

Maven

<dependency>
  <groupId>clojure.jdbc</groupId>
  <artifactId>clojure.jdbc-hikari</artifactId>
  <version>0.3.3</version>
</dependency>
only targets to java8. If you want use the java6-7 version of hikari-cp, you should setup all the dependencies manually.

Basic Usage

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.

Example using DBCP connection pool
;; Import the desired implementation
(require '[jdbc.pool.dbcp :as pool])

;; Convert the standard dbspec to an other dbspec with `:datasource` key
(def dbspec (pool/make-datasource-spec {:database-name "test"
                                        :adapter :postgresql}))

Configuration options

HikariCP comes with "good" defaults that should work in majority standard environments, but obviously, it exposes set of options for customize it:

Table 1. Specific generic options of HikariCP
OptionDescription

:auto-commit

This property controls the default auto-commit behavior of connections returned from the pool. It is a boolean value. (default true)

:read-only

This property controls whether Connections obtained from the pool are in read-only mode by default. (default `false)

:connection-timeout

This property controls the maximum number of milliseconds that a client will wait for a connection from the pool. If this time is exceeded without a connection becoming available, a SQLException will be thrown. 100ms is the minimum value. (default 10000)

:idle-timeout

This property controls the maximum amount of time (in milliseconds) that a connection is allowed to sit idle in the pool. (default 60000)

:max-lifetime

This property controls the maximum lifetime of a connection in the pool. A value of 0 indicates no maximum lifetime (infinite lifetime). (default 1800000)

:minimum-idle

This property controls the minimum number of idle connections that HikariCP tries to maintain in the pool. (default 10)

:maximum-pool-size

This property controls the maximum size that the pool is allowed to reach, including both idle and in-use connections. Basically this value will determine the maximum number of actual connections to the database backend. (default 10)

:adapter

This property sets the database adapter. Please check Adapters and corresponding datasource class names for the full list of supported adapters and their datasource class names. (this parameter is mandatory)

:username

This property sets the default authentication username used when obtaining Connections from the underlying driver. (default nil)

:password

This property sets the default authentication password used when obtaining Connections from the underlying driver. (default nil)

HikariCP, unlike other datasource implementations, requires to setup explicitly that adapter should be used. This is a list of supported adapters:

Table 2. List of adapters supported by HikariCP
AdapterDatasource class name

:derby

org.apache.derby.jdbc.ClientDataSource

:firebird

org.firebirdsql.pool.FBSimpleDataSource

:db2

com.ibm.db2.jcc.DB2SimpleDataSource

:h2

org.h2.jdbcx.JdbcDataSource

:hsqldb

org.hsqldb.jdbc.JDBCDataSource

:mariadb

org.mariadb.jdbc.MySQLDataSource

:mysql

com.mysql.jdbc.jdbc2.optional.MysqlDataSource

:sqlserver-jtds

net.sourceforge.jtds.jdbcx.JtdsDataSource

:sqlserver

com.microsoft.sqlserver.jdbc.SQLServerDataSource

:oracle

oracle.jdbc.pool.OracleDataSource

:pgjdbc-ng

com.impossibl.postgres.jdbc.PGDataSource

:postgresql

org.postgresql.ds.PGSimpleDataSource

:sybase

com.sybase.jdbcx.SybDataSource

Examples

Examples using HikariCP with h2 adapter.
(def dbspec
  (-> {:adapter :h2
       :url "jdbc:h2:/tmp/test"}
      (pool/make-datasource-spec)))
Example using HikariCP with postgresql adapter.
;; Convert the standard dbspec to an other dbspec with `:datasource` key
(def dbspec
  (-> {:database-name "test"
       :username "foo"
       :password "secret"
       :server-name "localhost"
       :port-number 5432
       :adapter :postgresql}
      (pool/make-datasource-spec)))

additional notes

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 controll and access to all options, consider using the dbcp directly as documented in http://niwibe.github.io/clojure.jdbc/latest/#connection-pool

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close