Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns testmultiple.core
- (:require
- [clojure.java.jdbc :as jdbc]
- [compojure.core :refer [defroutes GET ANY routes context]]
- [conman.core :as conman]
- [mount.core :refer [defstate]]))
- (def database-urls {:DB1 "jdbc:mysql://localhost:3306/DB1?user=DB1_user&password=DB1_password"
- :DB2 "jdbc:mysql://localhost:3306/DB2?user=DB2_user&password=DB2_password"})
- ;; Connects to all databases in pool-specs
- (defn connect!
- [pool-specs]
- (reduce merge (map (fn [pool-spec]
- {(keyword (key pool-spec)) (conman/connect! {:jdbc-url (val pool-spec)})}) pool-specs)))
- ;; Disconnect from all databases in db-connections
- (defn disconnect!
- [db-connections]
- (map (fn [db] (conman/disconnect! (val db))) db-connections))
- ;; Establish connections to all databases
- ;; and store connections in *dbs*
- (defstate ^:dynamic *dbs*
- :start (connect!
- database-urls)
- :stop (disconnect! *dbs*))
- ;; Bind queries to *db* dynamic variable which is bound
- ;; to each clients database before executing queries
- ;; The queries file defines the query get-user which
- ;; returns user by user id
- (def ^:dynamic *db* nil)
- (conman/bind-connection *db* "sql/queries.sql")
- (mount.core/start)
- ; Define function that executes in current *db* binding
- (defn getuser [id] (get-user {:id id}))
- ; Works, the user with Id 670 is returned from DB1
- (with-bindings {#'*db* (:DB1 *dbs*)} (getuser 670))
- ; Works, the user with Id 670 is returned from DB2
- (with-bindings {#'*db* (:DB2 *dbs*)} (getuser 670))
- (defn serve-page1 [] (str "page1" (getuser 670)))
- (defn serve-page2 [] (str "page2" (getuser 670)))
- (def home-routes
- (context "/:project" [project]
- (if (contains? *dbs* (keyword project))
- (routes
- (GET "/page1" []
- (with-bindings {#'*db* ((keyword project) *dbs*)}
- (serve-page1)))
- (GET "/page2" []
- (with-bindings {#'*db* ((keyword project) *dbs*)}
- (serve-page2))))
- (ANY "*" [] (str "Project not found")))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement