Best practice? Local vs global connections

25 views
Skip to first unread message

pinkwe...@gmail.com

unread,
Dec 29, 2013, 4:56:21 PM12/29/13
to congomo...@googlegroups.com
Hi, I'm pretty new to this mongodb business, and perhaps my question is more of a mongodb question than a congomongo question, however I may just lack the best way to phrase a google search. Anyway here goes.

I'm messing around using heroku and congomongo and It seems I can either set-connection! once globally, or use with-mongo. I'm wondering about best practices here are.

A casual glance shows setting the connection globally has some pros like setting a reference to *my-db* or similar and saves me a lot of repetitive code in my functions eliminating the with-mongo calls.

I have no clue about efficiency using set-connection! vs biding them on the fly using with-mongo. I'm curious about the difference between them when fetching and inserting values. Does with-mongo actually create a connection differently than set-connection! to the database 'behind the scenes'? Put another way, Is set-connection! used simply to store a global default connection (saving keystrokes) or is it somehow more efficient when talking to the mongodb server?

Sean Corfield

unread,
Dec 29, 2013, 5:57:02 PM12/29/13
to congomongo-dev
The general consensus with Clojure these days is to avoid reliance on
dynamically bound variables. I've just gone through a major overhaul
of the Clojure contrib JDBC wrapper, clojure.java.jdbc, to remove the
global *db* variable and provide a more functional, more idiomatic API
for that library. I plan to do the same with CongoMongo at some point.

Accordingly, I'd avoid set-connection! - it's likely to go away (it'll
be deprecated in the short term, and disappear in the long term) - and
use with-mongo calls although even those will change at some point
since they also rely on dynamically bound globals, however there
syntax is likely to change less.

Currently you'd have:

(with-mongo mongo-spec
... do a bunch of MongoDB stuff ...)

The API will likely change to:

(with-mongo [db mongo-spec]
... do a bunch of MongoDB stuff passing db into each call ...)

What I did with clojure.java.jdbc was introduce
clojure.java.jdbc.deprecated with the old API, and rework (and mostly
rename) what was in clojure.java.jdbc to be the new API. In a couple
of releases, clojure.java.jdbc.deprecated will go away completely
(since it will no longer be maintained anyway). I'll probably take a
similar approach with CongoMongo.

Having used CongoMongo extensively in production for a couple of
years, there are quite a few things I'd like to change about a number
of the API functions...

Sean
> --
> You received this message because you are subscribed to the Google Groups
> "congomongo-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to congomongo-de...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

pinkwe...@gmail.com

unread,
Dec 29, 2013, 7:37:46 PM12/29/13
to congomo...@googlegroups.com
Great, I'll favor with with-mongo then. Thanks for the help.

Kornelis Sietsma

unread,
Dec 30, 2013, 3:28:24 PM12/30/13
to congomo...@googlegroups.com
Glad to hear this - I was looking for just this sort of change.

It seemed strange that both major Clojure mongodb drivers use dynamic variables, and encourage global state - I guess it matches how Mongo works by default in the JavaScript console.  But it makes it hard to abstract out the database using something like Stuart Sierra's component stuff: https://github.com/stuartsierra/component

- Korny
Reply all
Reply to author
Forward
0 new messages