Shantanu Kumar avatar Shantanu Kumar committed fb1474c

add connection-properties support for DataSource

Comments (0)

Files changed (3)

 - [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

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

   datasource)
 
 
+(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).
   Arguments:
     db-path (String) for file   -- <filepath>
                      for memory -- :memory:
   Example:
-    (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:
     http://axion.tigris.org/quickstart.html"
   [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 )))
   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)

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

       (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})
       ds)))
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.