From a7ca1bad3b36efea5694342c728ff702276e45e6 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 19 Aug 2012 15:11:30 +0200 Subject: db: Make XA-capable. A non-Immutant fallback is provided. --- src/mulk/benki/db.clj | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'src') 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) -- cgit v1.2.3