 (ns com.puppetlabs.cmdb.scf.storage
-  (:import (com.jolbox.bonecp BoneCPDataSource BoneCPConfig))
+  (:import (com.jolbox.bonecp BoneCPDataSource BoneCPConfig)
+           (java.util.concurrent TimeUnit))
   (:require [com.puppetlabs.cmdb.catalog :as cat]
             [com.puppetlabs.utils :as utils]
             [clojure.java.jdbc :as sql]

       :dynamic true}
  *db* {:classname "org.h2.Driver"
        :subprotocol "h2"
        :subname "mem:cmdb;DB_CLOSE_DELAY=-1"})




(defn- new-connection-pool-instance
  "Create a new connection pool for the SCF database, configured appropriately,
and return it."
  [{:keys [subprotocol subname username password
           partition-conn-min partition-conn-max partition-count
           stats log-statements log-slow-statements]
    :or {partition-conn-min 1
         partition-conn-max 10
         partition-count    5
         stats              true}
    :as db}]
  (let [config (doto (new BoneCPConfig)
                 (.setDefaultAutoCommit false)
                 (.setLazyInit true)
                 (.setMinConnectionsPerPartition partition-conn-min)
                 (.setMaxConnectionsPerPartition partition-conn-max)
                 (.setPartitionCount partition-count)
                 (.setStatisticsEnabled stats)
                 ;; paste the URL back together from parts.
                 (.setJdbcUrl (str "jdbc:" subprotocol ":" subname)))]
    ;; configurable without default
    (when username (.setUsername config username))
    (when password (.setPassword config password))
    (when log-statements (.setLogStatementsEnabled config log-statements))
    (when log-slow-statements
      (.setQueryExecuteTimeLimit config log-slow-statements (TimeUnit/SECONDS)))
    ;; ...aaand, create the pool.
    (BoneCPDataSource. config)))

