Skip to content

Commit

Permalink
Add is-closed? and is-running? functions
Browse files Browse the repository at this point in the history
Plus a bunch of improvements:

- Closes tomekw#97
- A proper connection test with H2
- Updated documentation to use `next.jdbc`
  • Loading branch information
lukaszkorecki committed Nov 29, 2023
1 parent 002c252 commit dbc3dee
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 85 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Custom translations of properties can be added by extending the
```clojure
(ns hikari-cp.example
(:require [hikari-cp.core :refer :all]
[clojure.java.jdbc :as jdbc]))
[next.jdbc :as jdbc]))

(def datasource-options {:auto-commit true
:read-only false
Expand All @@ -124,7 +124,7 @@ Custom translations of properties can be added by extending the
(delay (make-datasource datasource-options)))

(defn -main [& args]
(jdbc/with-db-connection [conn {:datasource @datasource}]
(with-open [conn (jdbc/get-connection @datasource {})]
(let [rows (jdbc/query conn "SELECT 0")]
(println rows)))
(close-datasource @datasource))
Expand All @@ -134,15 +134,15 @@ Custom translations of properties can be added by extending the
```clojure
(ns hikari-cp.example
(:require [hikari-cp.core :refer :all]
[clojure.java.jdbc :as jdbc]))
[next.jdbc :as jdbc]))

(def datasource-options {:jdbc-url "jdbc:neo4j:bolt://host:port/?username=neo4j&password=xxxx&debug=true"})

(defonce datasource
(delay (make-datasource datasource-options)))

(defn -main [& args]
(jdbc/with-db-connection [conn {:datasource @datasource}]
(with-open [conn (jdbc/get-connection @datasource {})]
(let [rows (jdbc/query conn ["MATCH (n:Foo) WHERE n.name = ? RETURN n" "john"])]
(println rows)))
(close-datasource @datasource))
Expand All @@ -159,7 +159,7 @@ Custom translations of properties can be added by extending the
```clojure
(ns hikari-cp.example
(:require [hikari-cp.core :refer :all]
[clojure.java.jdbc :as jdbc]))
[next.jdbc :as jdbc]))

(def datasource-options {:username "username"
:password "password"
Expand All @@ -173,7 +173,7 @@ Custom translations of properties can be added by extending the
(delay (make-datasource datasource-options)))

(defn -main [& args]
(jdbc/with-db-connection [conn {:datasource @datasource}]
(with-open [conn (jdbc/get-connection @datasource {})]
(let [rows (jdbc/query conn "SELECT 0 FROM dual")]
(println rows)))
(close-datasource @datasource))
Expand All @@ -184,7 +184,7 @@ Custom translations of properties can be added by extending the
```clojure
(ns hikari-cp.example
(:require [hikari-cp.core :refer :all]
[clojure.java.jdbc :as jdbc]))
[next.jdbc :as jdbc]))

(def datasource-options {:username "username"
:password "password"
Expand All @@ -202,7 +202,7 @@ Custom translations of properties can be added by extending the
(delay (make-datasource datasource-options)))

(defn -main [& args]
(jdbc/with-db-connection [conn {:datasource @datasource}]
(with-open [conn (jdbc/get-connection @datasource {})]
(let [rows (jdbc/query conn "SELECT 0")]
(println rows)))
(close-datasource @datasource))
Expand Down Expand Up @@ -245,12 +245,12 @@ Custom translations of properties can be added by extending the
:max-lifetime 300000
:pool-name "clickhouse-conn-pool"})

(defonce datasource
(defonce datasource
(delay (make-datasource datasource-options)))

(defn -main
[& args]
(with-open [conn (jdbc/get-connection {:datasource @datasource})]
(with-open [conn (jdbc/get-connection @datasource {})]
(let [rows (jdbc/execute! conn ["SELECT 0"])]
(println rows)))
(close-datasource @datasource))
Expand All @@ -277,7 +277,7 @@ Custom translations of properties can be added by extending the

(defn -main
[& args]
(with-open [conn (jdbc/get-connection {:datasource @datasource})]
(with-open [conn (jdbc/get-connection @datasource {})]
(let [rows (jdbc/execute! conn ["SELECT 0"])]
(println rows)))
(close-datasource @datasource))
Expand Down
18 changes: 10 additions & 8 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
:url "https://github.com/tomekw/hikari-cp"}
:dependencies [[org.clojure/clojure "1.11.1" :scope "provided"]
[org.tobereplaced/lettercase "1.0.0"]
[com.zaxxer/HikariCP "5.0.1"]]
[com.zaxxer/HikariCP "5.1.0"]]
:global-vars {*warn-on-reflection* true}
:deploy-repositories [["clojars" {:sign-releases false :url "https://clojars.org/repo"}]]
:profiles {:dev {:dependencies [[expectations "2.1.10"]
[org.slf4j/slf4j-nop "1.7.36"]
[org.clojure/java.jdbc "0.7.12"]
[mysql/mysql-connector-java "8.0.30"]
[org.neo4j.driver/neo4j-java-driver "5.0.0"]
[org.postgresql/postgresql "42.5.0"]
[io.dropwizard.metrics/metrics-core "4.2.12"]
[io.dropwizard.metrics/metrics-healthchecks "4.2.12"]
[org.slf4j/slf4j-nop "2.0.9"]
[com.github.seancorfield/next.jdbc "1.3.894"]
[mysql/mysql-connector-java "8.0.33"]
[org.neo4j.driver/neo4j-java-driver "5.15.0"]
[org.postgresql/postgresql "42.7.0"]
[io.dropwizard.metrics/metrics-core "4.2.22"]
[io.dropwizard.metrics/metrics-healthchecks "4.2.22"]
[io.prometheus/simpleclient "0.16.0"]
[com.h2database/h2 "2.2.224"]

; The Oracle driver is only accessible from maven.oracle.com
; which requires a userId and password
Expand Down
12 changes: 11 additions & 1 deletion src/hikari_cp/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@

(defn datasource-config
"Create datasource config from `datasource-options`"
[datasource-options]
^HikariConfig [datasource-options]
(let [config (HikariConfig.)
options (validate-options datasource-options)
not-core-options (apply dissoc options core-options)
Expand Down Expand Up @@ -273,3 +273,13 @@
"Close given `datasource`"
[^HikariDataSource datasource]
(.close datasource))

(defn is-running?
"Check if given `datasource` is running"
[^HikariDataSource datasource]
(.isRunning datasource))

(defn is-closed?
"Check if given `datasource` is closed"
[^HikariDataSource datasource]
(.isClosed datasource))
32 changes: 32 additions & 0 deletions test/hikari_cp/connection_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns hikari-cp.connection-test
(:require
[expectations :refer [expect]]
[hikari-cp.core :as hikari-cp]
[next.jdbc :as jdbc]))

(let [pool (hikari-cp/make-datasource {:adapter "h2"
:url "jdbc:h2:mem:test"
;; :register-mbeans true
;; :connection-timeout 1000
;; :connection-test-query "select 0"
})]

(with-open [connection (jdbc/get-connection pool {})]
(let [result (jdbc/execute-one! connection ["select 1 as count"])]
(expect {:COUNT 1} result)))

;; NOTE:
;; can't do this: (expect true (hikari-cp/is-running? pool))
;; most likely due to how expectations works
(let [running? (hikari-cp/is-running? pool)
closed? (hikari-cp/is-closed? pool)]
(expect true running?)
(expect false closed?))

(hikari-cp/close-datasource pool)

(let [running? (hikari-cp/is-running? pool)
closed? (hikari-cp/is-closed? pool)]

(expect false running?)
(expect true closed?)))
Loading

0 comments on commit dbc3dee

Please sign in to comment.