connect / disconnect to mongo db when app boots and then reuse connection in my functions

65 views
Skip to first unread message

Ben McFerren

unread,
Dec 2, 2015, 1:52:13 AM12/2/15
to Monger, a Clojure MongoDB driver
newbie question but I'm having trouble finding a definitive answer online

so far, I have been connecting to my mongodb in the let block of each of my function definitions.

(def mongo-uri "mongodb://<username:<password>@...")


(defn get-node [nodeid]
 
(let [_nodeid nodeid
        uri mongo
-uri
           
{:keys [conn db]} (mg/connect-via-uri uri)
        ncoll
"nodes"]


   
(mc/find-maps db ncoll {:_id _nodeid})  
))


after some research, I found out this may be why my app is running slow with my compose service: http://stackoverflow.com/questions/32539191/connecting-to-remote-mongodb-quite-slow

I found a few sources online that discuss how to reuse the connection but I am having trouble finding a example online that shows me how my function definitions can have access to the connection object if the let block is not nested inside the function definition. 


For example:

(let [uri               "mongodb://clojurewerkz/monger!:monger!@127.0.0.1/monger-test4"
      {:keys [conn db]} (mg/connect-via-uri "mongodb://127.0.0.1/monger-test4")])

Could you please help me find the correct syntax to use so that my functions can have access to reuse the connection?

I am very grateful for help you can offer.

All the best,

Ben

Fernando Dobladez

unread,
Dec 2, 2015, 7:23:00 AM12/2/15
to clojure...@googlegroups.com
Looks like what you want is to establish the connection when your app starts, keep a reference to that connection on a shared namespace and simply refer to it from your functions.
You can keep a reference to the connection in a var as in `(def conn ...)`, either directly or using an `(atom)`.

Now, this may be enough for small "scripts". However, for non-trivial apps you'd want something more sophisticated:




--
Documentation: http://clojuremongodb.info
Issues: https://github.com/michaelklishin/monger/issues
 
More Clojure libraries: http://clojurewerkz.org
---
You received this message because you are subscribed to the Google Groups "Monger, a Clojure MongoDB driver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-mongo...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-mongodb.
For more options, visit https://groups.google.com/d/optout.

Ben McFerren

unread,
Dec 2, 2015, 10:18:47 AM12/2/15
to clojure...@googlegroups.com
can you please write out some example symtax using (def conn ... as well as (atom) along with connecting by uri?
You received this message because you are subscribed to a topic in the Google Groups "Monger, a Clojure MongoDB driver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-mongodb/9cpmEpKbHT8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-mongo...@googlegroups.com.


--
Benjamin McFerren
Sea * Side Syndication
www.seasidesyndication.com
(415) 238-8167
(786) 357-1485
sending thoughts out to become waves

Reply all
Reply to author
Forward
0 new messages