1. Shantanu Kumar
  2. Clj-DBCP


Shantanu Kumar  committed fb1474c

add connection-properties support for DataSource

  • Participants
  • Parent commits 00a3c1f
  • Branches default

Comments (0)

Files changed (3)


View file
  • Ignore whitespace
 - [TODO] Test-cases for untested databases: Oracle, IBM DB2, SQL Server, Sybase
 - [TODO] Support Excel files using JDBC driver
 - [TODO] Support XADataSource - maybe use JOTM: http://jotm.objectweb.org/
+- [TODO] Support for new database: Mckoi and MckoiDDB
+- [TODO] Refactor API (and properties file) for memory, filesystem and network
 ## 0.6 / 2011-Dec-??
 - Improved support for MS-Excel workbooks via JDBC-ODBC bridge driver
-- [TODO] properties support in DataSource JDBC URL
-- [TODO] protocols for memory-connect, file-connect, net-connect
+- Connection-properties support for DataSource
 - Support for new database: MonetDB
-- Support for new database: Axion support
-- [TODO] Support for new database: MckoiDDB
+- Support for new database: Axion
 - Support for Java 7 (JDBC 4.1)
 - Use OSS-JDBC 0.6
 - Use Clojure 1.3.0 and modular contrib

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

View file
  • Ignore whitespace
+(defn as-str
+  "Return string form of `x`."
+  [x]
+  (if (or (keyword? x) (symbol? x))
+    (name x)
+    (str x)))
+(defn add-connection-property!
+  "Add custom connection propert/y(ies) to the set that will be passed to our
+  JDBC driver. Both `k` (key) and `v` (value) are converted to string. `m` must
+  be either a map, or a comma-delimited string where key-value are in the form
+  'key=value' eg. \"socket_timeout=30000,reconnect_attempts=4\".
+  NOTE: You must call this *before* creating any connection from the
+        datasource. Calling this function after the connection pool has been
+        initialized will have no effect."
+  ([^BasicDataSource datasource ^String k ^String v]
+    (doto datasource
+      (.addConnectionProperty (as-str k) (as-str v)))
+    datasource)
+  ([^BasicDataSource datasource m] {:pre [(or (map? m) (string? m))]}
+    (cond
+      (map? m)    (doseq [[k v] m]
+                    (add-connection-property! datasource k v))
+      (string? m) (let [tokens (sr/split m #",")
+                        pairs  (map #(sr/split % #"=") tokens)]
+                    (doseq [[k v] pairs]
+                      (add-connection-property! datasource
+                                                (sr/trim k) (sr/trim v))))
+      :otherwise  (throw (IllegalArgumentException.
+                           (format "Expected map or string but found '%s': %s"
+                                   (type m)
+                                   (pr-str m)))))
+    datasource))
 (defn make-datasource
   "Create basic data source (instance of the BasicDataSource class).
     db-path (String) for file   -- <filepath>
                      for memory -- :memory:
-    (sqlite-args \"C:/work/mydatabase.db\")
-    (sqlite-args \":memory:\")
+    (axion-args \"mydatabase.sb\" \"C:/work\")
+    (axion-args \"employee.db\"   nil)
   Maven/Lein/Cake Dependencies:
-    org.xerial/sqlite-jdbc/3.7.2 (or a later version)
+    axion/axion/1.0-M3-dev (or a later version)
   See also:
   [db-name db-path]
 (defn apply-extra-args
   [^BasicDataSource datasource props]
+  ;; set-max-active!
   (when (contains? props :max-active)
-    (set-max-active! datasource (Integer/parseInt (:max-active props))))
+    (let [v (:max-active props)]
+      (when *show-jdbc-url* (println ":max-active" (pr-str v)))
+      (set-max-active! datasource (Integer/parseInt v))))
+  ;; set-min-max-idle!
   (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 *show-jdbc-url* (println ":min-max-idle" ms mx))
+      (set-min-max-idle! datasource
+                         (Integer/parseInt ms) (Integer/parseInt mx))))
+  ;; set-validation-query!
   (when (contains? props :validation-query)
-    (set-validation-query! datasource (:validation-query props)))
+    (let [v (:validation-query props)]
+      (when *show-jdbc-url* (println ":validation-query" v))
+      (set-validation-query! datasource v)))
+  ;; add-connection-property!
+  (when (contains? props :conn-properties)
+    (let [v (:conn-properties props)]
+      (when *show-jdbc-url* (println ":conn-properties" v))
+      (add-connection-property! datasource )))
    :h2-memory          (partial on-memory h2-memory-datasource)
    :hsql-memory        (partial on-memory hsql-memory-datasource)
    :sqlite-memory      (partial on-memory sqlite-memory-datasource)
+   :axion-memory       (partial on-memory axion-memory-datasource)
    ;; filesystem
    :derby-filesystem   (partial on-filesystem derby-filesystem-datasource)
    :h2-filesystem      (partial on-filesystem h2-filesystem-datasource)
    :hsql-filesystem    (partial on-filesystem hsql-filesystem-datasource)
    :sqlite-filesystem  (partial on-filesystem sqlite-filesystem-datasource)
+   :axion-filesystem   (partial on-filesystem axion-filesystem-datasource)
    ;; network
    :derby-network      (partial on-network derby-network-datasource)
    :h2-network         (partial on-network h2-network-datasource)

File src/test/clj/org/bituf/test_clj_dbcp.clj

View file
  • Ignore whitespace
       (set-max-active! ds 15)
       ;; set minimum and maximum number of idle connections on the datasource
       (set-min-max-idle! ds 3 8)
+      ;; set connection properties
+      (add-connection-property! ds "WRITE_DELAY=500, PAGE_SIZE=8192")
+      ;(add-connection-property! ds {:write_delay 500 :page_size 8192})