summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <code@mail.matthias.benkard.de>2012-08-19 15:11:30 +0200
committerMatthias Andreas Benkard <code@mail.matthias.benkard.de>2012-08-19 15:11:30 +0200
commita7ca1bad3b36efea5694342c728ff702276e45e6 (patch)
treefdbd1d34a1b0a58f38df3e105278e99dec3f8840
parentc4a725948d2b5de6e65d4cd2dc08d3c25dd1c431 (diff)
db: Make XA-capable.
A non-Immutant fallback is provided.
-rw-r--r--resources/config.sexp.sample7
-rw-r--r--src/mulk/benki/db.clj41
2 files changed, 43 insertions, 5 deletions
diff --git a/resources/config.sexp.sample b/resources/config.sexp.sample
index 804117d..a1e3a36 100644
--- a/resources/config.sexp.sample
+++ b/resources/config.sexp.sample
@@ -1,8 +1,9 @@
;;;; -*- mode: clojure; coding: utf-8 -*-
-{:database {:classname "org.postgresql.Driver"
- :subprotocol "postgresql"
- :subname "//localhost:5432/benki"
+{:database {;; NOTE: PostgreSQL is the only supported RDBMS.
+ :host "localhost"
+ :port 5432
+ :database "benki"
:user "benki"
:password ""}
:websocket-base "ws://localhost:3001"
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)