diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2012-08-19 15:11:30 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2012-08-19 15:11:30 +0200 |
commit | a7ca1bad3b36efea5694342c728ff702276e45e6 (patch) | |
tree | fdbd1d34a1b0a58f38df3e105278e99dec3f8840 /src | |
parent | c4a725948d2b5de6e65d4cd2dc08d3c25dd1c431 (diff) |
db: Make XA-capable.
A non-Immutant fallback is provided.
Diffstat (limited to 'src')
-rw-r--r-- | src/mulk/benki/db.clj | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/src/mulk/benki/db.clj b/src/mulk/benki/db.clj index b45ec30..95bceab 100644 --- a/src/mulk/benki/db.clj +++ b/src/mulk/benki/db.clj @@ -1,11 +1,48 @@ (ns mulk.benki.db (:refer-clojure) - (:use [mulk.benki config]) + (:use [mulk.benki config] + [clojure.pprint :only [cl-format]]) (:require [clojure.java.jdbc :as sql])) +(defn ^:private classic-db [] + (let [{host :host, port :port, database :database, + user :user, password :password} + (:database @benki-config)] + {:classname "org.postgresql.Driver" + :subprotocol "postgresql" + :subname (cl-format nil "//~A:~D/~A" host port database) + :user user + :password password})) + +(defonce ^:private +immutant-db+ (atom nil)) + +(defn ^:private immutant-db [] + (if @+immutant-db+ + {:datasource @+immutant-db+} + (let [{host :host, port :port, database :database, + user :user, password :password} + (:database @benki-config)] + (when (try + (require 'immutant.xa) + true + (catch Exception e + false)) + (let [datasource (ns-resolve 'immutant.xa 'datasource)] + (reset! +immutant-db+ + (datasource "benkidb" + {:adapter "postgres" + :host host + :port port + :database database + :username user + :password password + :pool 1})) + {:datasource @+immutant-db+}))))) + (defn ^:private db [] - (:database @benki-config)) + (or (immutant-db) (classic-db))) + (defn call-with-db [thunk] (sql/with-connection (db) |