Hi Pablo!!!!

I think the reason you've got so many responses is because most people 
share the same problem. I do think this is a fruitful area of discussion. 
There are multiple ways to go about it.

Based on your original post, I'll share my two cents:

In my experience, mixing an atom and dynamic vars is unnecessary and brings 
complexity that is not worth it. It would be better to separate the two. 
That is, use the atom, which would allow you to change the state (to 
reconnect, etc). But also use the dynamic var so that the dynamic scope is 
changed. That means that you may want to define a macro (but may also do 
without);

(def current-connection (atom nil))
(def ^:dynamic db nil)

;; somewhere later
(binding [db @current-connection]
  (do-some-db-operations))

Transactions could re-bind `db` in their dynamic scope.

Rebinding a dynamic var to a new atom is a little weird. It mixes mutation 
with dynamic scope. I'm sure there are weird edge cases that are hard to 
reason about. This way unifies the operations (even the first db operation 
needs a binding) and separates it from mutation, which appears necessary in 
your code.

The other thing is that you can make a nice Ring middleware like this:

(defn wrap-db [handler]
  (fn [req]
    (binding [db @current-connection]
      (handler req))))

The connection it gets at the beginning will be with it throughout, so you 
won't ever have a weird case where the connection atom is changed in the 
middle.

Thanks
Eric

On Thursday, August 6, 2015 at 11:56:49 AM UTC-5, J. Pablo Fernández wrote:
>
>
> On 5 August 2015 at 19:33, James Reeves <ja...@booleanknot.com 
> <javascript:>> wrote:
>
>> So when you're testing, presumably you use a dynamic binding to override 
>> the global connection to the test database?
>>
>
> The wrap transaction always overrides the dynamic binding, whether it's in 
> tests or not.
>
> -- 
> J. Pablo Fernández <pup...@pupeno.com <javascript:>> (http://pupeno.com)
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to