-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_pool.clj
30 lines (28 loc) · 1.01 KB
/
connection_pool.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(ns utility-belt.sql.component.connection-pool
(:require [hikari-cp.core :as hikari]
[clojure.tools.logging :as log]
[com.stuartsierra.component :as component]))
;; To be accepted as a db-spec (for JDBC)
;; component has to have a datasource key
(defrecord HikariCP [config datasource]
component/Lifecycle
(start [this]
(log/infof "%s connecting=%s %s:%s"
(:pool-name config)
(:database-name config)
(:server-name config)
(:port-number config))
;; we can't pass arbitrary config so...
(let [pool (hikari/make-datasource config)]
(assoc this :datasource pool)))
(stop [this]
(log/warnf "%s disconnecting=%s %s:%s"
(:pool-name config)
(:database-name config)
(:server-name config)
(:port-number config))
(when datasource
(hikari/close-datasource datasource))
(assoc this :datasource nil)))
(defn create [config]
(map->HikariCP {:config config}))