Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Modified open-global to be able to re-open a connection, respect the …

…old behavior if the db-spec is marked as safe.
  • Loading branch information...
commit 9394513c8f6482ad92f3e48f13d2e4da7b46d1ca 1 parent acc055c
@budu budu authored
Showing with 37 additions and 18 deletions.
  1. +32 −17 src/lobos/connectivity.clj
  2. +5 −1 test/lobos/test/connectivity.clj
View
49 src/lobos/connectivity.clj
@@ -56,23 +56,6 @@
;; ## Global Connections
-(defn open-global
- "Opens a global connection to the database specified by `db-spec`. If
- no `connection-name` is given, opens a default global connection."
- ([db-spec] (open-global :default-connection db-spec))
- ([connection-name db-spec]
- (if-let [cnx (connection-name @global-connections)]
- (throw
- (Exception.
- (format "A global connection by that name already exists (%s)"
- connection-name)))
- (let [cnx (get-cnx db-spec)]
- (when-let [ac (-> db-spec :auto-commit)]
- (.setAutoCommit cnx ac))
- (swap! global-connections assoc
- (or connection-name :default-connection)
- {:connection cnx :db-spec db-spec})))))
-
(defn close-global
"Supplied with a keyword identifying a global connection, that connection
is closed and the reference dropped."
@@ -88,6 +71,38 @@
(Exception. (format "No global connection by that name is open: %s"
connection-name))))))
+(defn- open-global* [connection-name db-spec]
+ (let [cnx (get-cnx db-spec)]
+ (when-let [ac (-> db-spec :auto-commit)]
+ (.setAutoCommit cnx ac))
+ (swap! global-connections assoc
+ (or connection-name :default-connection)
+ {:connection cnx :db-spec db-spec})))
+
+(defn open-global
+ "Opens a global connection to the database specified by `db-spec`. If
+ no `connection-name` is given, opens a default global connection.
+
+ If a global connection by that name already exists and the db-spec is
+ safe (see below), then an exeption will be thrown. When the db-spec is
+ not safe it will be closed if the old db-spec is different and the
+ original connection is left untouched.
+
+ A safe db-spec is a a map containing a :safe key set to a non-false
+ value."
+ ([db-spec] (open-global :default-connection db-spec))
+ ([connection-name db-spec]
+ (if-let [cnx (connection-name @global-connections)]
+ (if (:safe (:db-spec cnx))
+ (throw
+ (Exception.
+ (format "A global connection by that name already exists (%s)"
+ connection-name)))
+ (when-not (= (:db-spec cnx) db-spec)
+ (close-global connection-name)
+ (open-global* connection-name db-spec)))
+ (open-global* connection-name db-spec))))
+
;; -----------------------------------------------------------------------------
;; ## With Connections
View
6 test/lobos/test/connectivity.clj
@@ -35,8 +35,12 @@
(is (= @global-connections {})
"Closing a named global connection")
(open-global :foo {})
+ (open-global :foo {:safe true})
+ (is (= @global-connections
+ {:foo {:connection *cnx* :db-spec {:safe true}}})
+ "Re-opening a named global connection")
(is (thrown? Exception
- (open-global nil :foo))
+ (open-global :foo nil))
"Opening a global connection with a existing name")
(close-global :foo)
(is (thrown? Exception
Please sign in to comment.
Something went wrong with that request. Please try again.