Tuesday, 30 May 2017

i m trying to create the connection pooling in nodejs with mongodb but here not close after open 5 connection

It is a Index.js File Which Inside the app/db folder. .

****./app/db/index.js**** 

    var MongoClient = require('mongodb').MongoClient;
    var assert=require('assert');`enter code here`
    var mongo = require("mongodb");

    var state = {
        db: null,
      }

     var option = {
       db:{
         numberOfRetries : 5
       },
       server: {
         auto_reconnect: false,
        poolSize : 5,
         socketOptions: {
             connectTimeoutMS: 500
         }
      },
       replSet: {},
       mongos: {}
     };
      exports.connect=function(done){
                 setTimeout(function(){
                    MongoClient.connect(dburl, option,function(err, db) {
                             assert.equal(null, err);
                                 state.db=db;
                             db.listCollections({}).toArray(function(err, collections) {
                         assert.equal(null, err);                        collections.forEach(function(collection) {
                         console.log(collection);
                             });
                         });
                         return done();
                         db.close();

                    });
                }); 
      }


    exports.get = function() {
      return state.db

.....................................................................................................................................................................This is a Main . .
****server.js****

    var express   =     require("express");
    var s=require('session');
    var bodyParser=require("body-parser");
    var app=express();
    var mongoClient=require('mongodb').MongoClient;
    var rt=require('./app');
    var db=require('./app/db');

    db.connect(function(err) {
    if (err) {
         console.log('Unable to connect to Mongo.')
         process.exit(1)
     }else{
         console.log("database connected");
     }
    });`
`
please give me suggestion And solution



via mohd amir

No comments:

Post a Comment