无法使用hikari-cp创建连接池

时间:2018-07-31 12:39:59

标签: clojure hikaricp

我无法在使用Clojure代码的应用程序中使用hikari-cp库创建连接池。

我尝试了以下代码:

(def ^:private connection-pool (atom nil))
(defn initialize! []
  (reset!
    connection-pool
    {:datasource (hikari/make-datasource (config/db-spec))}))

config / db-spec正在获取适当的配置参数,因为我能够使用我用于make-datasource的参数连接到PostgreSQL。

我正在使用[hikari-cp“ 1.6.1”]

并且我遇到以下错误:

[INFO ] 2018-07-31 18:03:18,735 [main] com.zaxxer.hikari.HikariDataSource:<init>: db-pool - is starting.
[INFO ] 2018-07-31 18:03:48,845 [main] com.zaxxer.hikari.pool.HikariPool:shutdown: db-pool - is closing down.
Exception in thread "main" com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Exception during pool initialization: db-pool - Connection is not available, request timed out after 30009ms., compiling:<>
at com.zaxxer.hikari.pool.HikariPool.initializeConnections(HikariPool.java:526)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:136)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:71)
    at hikari_cp.core$make_datasource.invokeStatic(core.clj:183)
    at hikari_cp.core$make_datasource.invoke(core.clj:180)

1 个答案:

答案 0 :(得分:0)

这里是an example repo,用于将Hikari和JDBC与H2本地数据库一起进行测试。它使用当前版本:[hikari-cp "2.3.0"]

只需克隆存储库并运行lein test

~/expr/demo-jdbc > lein test

lein test _bootstrap

----------------------------------
   Clojure 1.9.0    Java 10.0.1
----------------------------------

lein test demo.core

lein test tst.demo.core

lein test tst.demo.jdbc

result-0 => 
({:lang "Clojure", :desc "ancients"}
 {:lang "Clojure", :desc "1.8"}
 {:lang "Clojure", :desc "1.9"})

lein test tst.demo.jdbc-pool

all tests finished

Ran 4 tests containing 10 assertions.
0 failures, 0 errors.

相关部分是:

(ns xyz
  (:require
    [clojure.java.jdbc :as jdbc]
    [hikari-cp.core :as pool]
    ))

(def datasource-options-sample {:auto-commit        true
                                :read-only          false
                                :connection-timeout 30000
                                :validation-timeout 5000
                                :idle-timeout       600000
                                :max-lifetime       1800000
                                :minimum-idle       10
                                :maximum-pool-size  10
                                :pool-name          "db-pool"
                                :adapter            "h2" ; "postgresql"
                                :username           "sa"
                                :password           ""
                                :database-name      "database"
                                :server-name        "localhost"
                                :port-number        5432
                                :register-mbeans    false})

(def datasource-options {:adapter  "h2"
                         :url      "jdbc:h2:mem:demo;DB_CLOSE_DELAY=-1"
                         :username "sa"
                         :password ""})

(def ^:dynamic db-conn nil)

(defn with-connection-pool
  "Creates and uses a connection for test function"
  [tst-fn]
  (let [datasource (pool/make-datasource datasource-options)]
    (binding [db-conn {:datasource datasource}]
      (tst-fn)
      (pool/close-datasource datasource)))) ; close the connection - also closes/destroys the in-memory database

(use-fixtures
  :once with-connection-pool) ; use the same db connection pool for all tests