Question about connecting to an external dbase server

110 views
Skip to first unread message

EJ Jaquet

unread,
Jul 16, 2011, 10:09:44 AM7/16/11
to CFMongoDB
Hi all,

I am trying to get cfmongodb working in Coldbox. I have it set up
through Wirebox and i can connect to the local mongoDB server fine.

But for production I want to connect to a db that is hosted by
mongoHQ. That is on another server and it is using auhenication. I
have found some examples about how to accomplish this, but none of
them are working for me at the moment.

Can someone point me to a good example or show me how to connect to an
external db with authentication?

Also, i was wondering which way is better, i am now using the
javaloader but saw that you can also put the jars into the ColdFusion
classpath. Which way is better, safer, faster?

Hope you can help,

Kind regards

Erik-Jan

Marc Esher

unread,
Jul 16, 2011, 10:45:13 AM7/16/11
to cfmo...@googlegroups.com
EJ,

For an external server, you *should* be able to configure it as normal:

<cfset hosts = [{serverName='IP.For.Mongo.HQ.Server', serverPort=
port.for.mongo.hq.server]>
<cfset mongoConfig = createObject('component',
'cfmongodb.core.MongoConfig').init( hosts = hosts, dbname='whatever',
mongoFactory=javaloaderFactory );
<cfset mongo = createObject(..).init(mongoConfig);

and then authenticate:

authResult = mongo.authenticate('yourusername', 'yourpassword');


IF that doesn't work for you, please let me knwo what errors you
receive. cfdump authresult to find out what mongodb has to say.

As for whether to use javaloader vs. putting the jars in the
classpath, I'm afraid the best answer is: "try it out and see which is
faster/better for you".

I can tell you that I run javaloader in production, though I'm not
running Twitter type loads. In addition, I built the "get object" part
of CFMongoDB to be optimized for use with javaloader, such that it
should be nearly as fast to use javaloader in typical situations.

Fortunately, it's easy to try: put the mongodb and cfmongodb jars in
your CF classpath, restart, and change your wirebox config to NOT use
the javaloader factory. Basically, just remove the MongoFactory from
your map, and map the mongoconfig like so:

map("mongoConfig")
.to("cfmongodb.core.MongoConfig")
.initArg(name="dbName",value="mongorocks")
.initArg(hosts=[.....])
.asSingleton();

Marc

> --
> You received this message because you are subscribed to the Google Groups "CFMongoDB" group.
> To post to this group, send email to cfmo...@googlegroups.com.
> To unsubscribe from this group, send email to cfmongodb+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cfmongodb?hl=en.
>
>

C.J. McCarey

unread,
Jul 16, 2011, 11:33:05 AM7/16/11
to cfmo...@googlegroups.com

One other thigh to add to what Marc said, make sure to double check any firewalls you have setup to make sure you aren't getting shutdown there.

EJ Jaquet

unread,
Jul 16, 2011, 12:28:32 PM7/16/11
to CFMongoDB
Hi Marc, C.J,

Thanks for your replies.

I figured out the server part, and now have this in my Wirebox.cfc:

map("mongoFactory")
.to("model.utilities.cfmongodb.core.JavaloaderFactory")
.asSingleton();
//use cfmongodb.core.DefaultFactory to load mongodb jars in your CF
classpath

map("mongoConfig")
.to("model.utilities.cfmongodb.core.MongoConfig")
.initArg(name="dbName",ref="mongoDBSetting")
.initArg(name="mongoFactory", ref="mongoFactory")
.initArg(name="hosts", ref="mongoDBHost")
.asSingleton();

map("mongo")
.to("model.utilities.cfmongodb.core.Mongo")
.initArg(name="mongoConfig", ref='mongoConfig')
.asSingleton();

If I manually add my username and password to the Mongoconfig file, I
can use the DB fine. So all is working.
But of course, I want the username and password to come from the
coldbox config file.

So how do I call the authresult function from the Wirebox.cfc, that is
the last part I am trying to resolve...

Thanks for all your help!

Erik-Jan

On 16 jul, 17:33, "C.J. McCarey" <cj.mcca...@gmail.com> wrote:
> One other thigh to add to what Marc said, make sure to double check any
> firewalls you have setup to make sure you aren't getting shutdown there.

Marc Esher

unread,
Jul 16, 2011, 12:44:19 PM7/16/11
to cfmo...@googlegroups.com

I'm no wirebox expert, but I don't think you would call it from your
mapping file, but instead you'd call it after wirebox does its config.
Something like:

<cfset binder = createObject("component", "Binder")>
<cfset application.injector =
createObject("component","coldbox.system.ioc.Injector").init("Binder")>

<cfset mongo = application.injector.getInstance("mongo")>
<cfset mongo.authenticate(username, password)>

Presumably at that point, you could get the username and password from
your coldbox config file.

Marc

Nolan Dubeau

unread,
Feb 3, 2012, 10:19:17 AM2/3/12
to cfmo...@googlegroups.com
Hi Erik,

I'm in the process of setting up my first CB app with ColdBox.  Just wondering how you progressed with this.

So far I've been able to get Wirebox configured like so:

 map("mongoFactory")
.to("model.utilities.cfmongodb.core.JavaloaderFactory")
.asSingleton();
//use cfmongodb.core.DefaultFactory to load mongodb jars in your CF classpath


map
("mongoConfig")
.to("model.utilities.cfmongodb.core.MongoConfig")

.initArg(name="dbName",value="mongorocks")

.initArg(name="mongoFactory", ref="mongoFactory")

.asSingleton();


map("mongo")
.to("model.utilities.cfmongodb.core.Mongo").
initArg(name="mongoConfig", ref='mongoConfig')
.asSingleton();

}

I'm struggling to understand your mongo config, can you clarify?

 map("mongoConfig") 
                        .to("model.utilities.cfmongodb.core.MongoConfig") 
                        .initArg(name="dbName",ref="mongoDBSetting") 
                        .initArg(name="mongoFactory", ref="mongoFactory") 
                        .initArg(name="hosts", ref="mongoDBHost") 
                        .asSingleton(); 

Questions:

1. What does ref="mongoDBSetting" refer to?  A setting in ColdBox Config?
2. What does the last initArgs for mongoDBHost do?

I'm not 100% familiar with Wirebox, so I'm going to read through the docs again.

Essentially I'm looking to build a config to leverage CB's environment settings and have wirebox map mongo to my local db when running in development, and a remote hosted mongodb when in the production environment.

Thanks for your assistance.

Nolan

EJ Jaquet

unread,
Feb 7, 2012, 2:04:22 PM2/7/12
to cfmo...@googlegroups.com
Hi Nolan,

I did manage to get MongoDB working in Coldbox, via Wirebox. I ended up making a small change to the init function of the cfmongodb project, where I could pass in a hostname. In my Coldbox settings, I entered the hostname in mongoDBHost, a different host for the prodution and development environment. So, just as you are aiming for?

The code in this post, is still the code I am using if I am not mistaking. Only thing I did not figure out at the time, was how to properly close the MongoDB connection in the request.

Let me know if you have furhter questions, and if it helps, I can mail my code, as soon as I get home.

Kind regards,

Erik-Jan

Marc Esher

unread,
Feb 8, 2012, 9:03:26 PM2/8/12
to cfmo...@googlegroups.com
EJ,

Thanks.

You did figure out how to close the connection, right? Absolutely you
should not be closing connections on each request, but only on
application stop (or restart)!

Marc

> --
> You received this message because you are subscribed to the Google Groups
> "CFMongoDB" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/cfmongodb/-/9UGNp1hUv2EJ.

Nolan Dubeau

unread,
Apr 3, 2013, 11:51:50 AM4/3/13
to cfmo...@googlegroups.com
Hi Erik,

I'm back on this project again.  I had to abandon going with MongoDB at the time and now I have another requirement to get this working.  I was wondering if you would be able to share the modifications you made to get Mongo working with an external hosting company as well as how you integrated your ColdBox environment settings with Wirebox.  I'm trying to do exactly the same and have not been able to get it to work.

Thanks very much.

Nolan
Reply all
Reply to author
Forward
0 new messages