How to set connection string from session variable and prevent new db connection on request

48 views
Skip to first unread message

Moyo Falaye

unread,
Mar 24, 2014, 2:56:06 PM3/24/14
to mongoo...@googlegroups.com
Hi everyone. Please i need help.

I am trying to create a connection to a database based on data retrieved from another database.
This is how i tried to tackle this task

  1. used a middle-ware to seek out and set the database info in session based on req subdomain.
  2. used another middle-ware to create clients db connection. Like this
  • if(typeof clientdbconn === 'undefined'){
        console
    .log('before init2 client conn ' + clientdbconn);
        app
    .use(function(req, res, next){
           
    if(req.session.Client && req.session.Client.name === req.subdomains[0] ){            
               
    var client = mongoose.createConnection(req.session.Client.dbUrl /*, dbconfigoptions*/);
                client
    .on('connected', function () {
                    console
    .log('Mongoose default connection open to  Client');
               
    });
                clientdbconn
    = client;
                console
    .log('client connection established, and saved ' + clientdbconn);
               
    next();
           
    }
           
    else{
               
    delete req.session.Client;
                client
    = false;
               
    next();
           
    }
       
    });
    }Enter code here...


     3.  created model with reference to the above connection (clientdbconn)

  • usersmodel = new Schema({
        name      
    : String
    });
    User: clientdbconn.model('User', usersmodel, 'users');

This Partially works, in that the connection is made to the database and i can query. The problem is that on every request, a new connection is made. ( does not take advantage of persistence pooling).

I tried to store the connection outside of the middle ware in my app.js as a variable object like this

var clientdbconn;

and if you look at the middleware above I store the connection in the var clientdbconn;
 

But even after doing that, upon a new request the var clientdbconn; is set to undefined there fore  making the middleware to create a new db connection.


Please what is the proper way to do this ?

create a new database connection during run time, and storing the connection for subsequent requests so as not to create new connections on every request unless  the connection string changes.


Thanks in advance

Mark Krieger

unread,
May 15, 2014, 10:52:42 AM5/15/14
to mongoo...@googlegroups.com
I'm trying to do a very similar thing. Have you found a way to do this yet? 

Joe Wagner

unread,
May 15, 2014, 3:03:33 PM5/15/14
to mongoo...@googlegroups.com
Without seeing more code its hard to say what the issue is, but a general solution to this might be to create a class that exposes a findOrCreate for the different mongoose database connections, serialized by the connection string.
If you try to get a connection that doesn't exist or is disconnected a new one would be created, and the correct models would be attached.

Here's a simple example of what I'm suggesting.  Disclaimer: I didn't test this at all, and it could certainly be generalized for how the models are attached, but I think it gets the idea across.

I would love to hear if anyone else has a different solution to this.

Hope that helps,
Joe
Reply all
Reply to author
Forward
0 new messages