Commits

Shantanu Kumar committed 8c1772b

add `apply-extra-args` fn to tune the datasource

  • Participants
  • Parent commits c6824f2

Comments (0)

Files changed (1)

src/main/clj/org/bituf/clj_dbcp.clj

     (org.apache.commons.dbcp BasicDataSource))
   (:require
     [clojure.java.io :as io]
-    [clojure.pprint  :as pp]))
+    [clojure.pprint  :as pp]
+    [clojure.string  :as sr]))
 
 
 (def ^{:doc "Clj-DBCP version - major and minor only"}
     (zipmap (map #(keyword (.substring ^String % pc)) ks) vs)))
 
 
+(defn apply-extra-args
+  [^BasicDataSource datasource props]
+  (when (contains? props :max-active)
+    (set-max-active! datasource (Integer/parseInt (:max-active props))))
+  (when (and (contains? props :min-max-idle))
+    (let [[ms mx] (sr/split (:min-max-idle props) #",")]
+      (set-min-max-idle! datasource (Integer/parseInt ms) (Integer/parseInt mx))))
+  (when (contains? props :validation-query)
+    (set-validation-query! datasource (:validation-query props)))
+  datasource)
+
+
 (defn on-memory
   "Helper function to create datasource (on memory) with default configuration."
   [factory-fn props]
            :pre  [(map? props)
                   (every? keyword? (keys props))
                   (every? string?  (vals props))]}
-    (cond
-      ;; supported databases
-      (and (contains? props :db-type)
-          (contains? supported-dbs
-            (keyword
-              (:db-type props))))        ((get supported-dbs (keyword
-                                                               (:db-type props)))
-                                        props)
-      ;; JDBC-ODBC bridge
-      (contains? props :odbc-dsn)     (on-odbc props)
-      ;; JDBC Driver
-      (contains? props :jdbc-url)     (on-jdbc props)
-      ;; Else
-      :else                           (throw
-                                        (IllegalArgumentException.
-                                          (format
-                                            "Expected either of keys `db-type`,
+    (-> (cond
+          ;; supported databases
+          (and (contains? props :db-type)
+              (contains? supported-dbs
+                (keyword
+                  (:db-type props))))        ((get supported-dbs
+                                                (keyword (:db-type props)))
+                                            props)
+          ;; JDBC-ODBC bridge
+          (contains? props :odbc-dsn)     (on-odbc props)
+          ;; JDBC Driver
+          (contains? props :jdbc-url)     (on-jdbc props)
+          ;; Else
+          :else                           (throw
+                                            (IllegalArgumentException.
+                                              (format
+                                                "Expected either of keys `db-type`,
 `odbc-dsn` and `jdbc-url` but found %s Valid db-type values are: %s"
-                                            (with-out-str (pp/pprint props))
-                                            (with-out-str
-                                              (pp/pprint
-                                                (keys supported-dbs))))))))
+                                                (with-out-str (pp/pprint props))
+                                                (with-out-str
+                                                  (pp/pprint
+                                                    (keys supported-dbs)))))))
+      (apply-extra-args props)))
   ([] {:post [(instance? DataSource %)]}
     (let [props (load-datasource-args)]
       (make-datasource-from-properties props))))