loopback-example-database icon indicating copy to clipboard operation
loopback-example-database copied to clipboard

disconnection with mongodb server on using mongdb datasource

Open yunchancho opened this issue 11 years ago • 0 comments

When I connect 'Review' model of this example to mongodb datasource, this example is disconnected with mongodb server.

mongodb servers' logs are the followings.

2014-12-27T15:52:05.829+0900 [initandlisten] connection accepted from 127.0.0.1:57496 #18 (1 connection now open)
2014-12-27T15:52:05.843+0900 [conn18] end connection 127.0.0.1:57496 (0 connections now open)
2014-12-27T15:52:05.847+0900 [initandlisten] connection accepted from 127.0.0.1:57497 #19 (1 connection now open)
2014-12-27T15:52:05.848+0900 [initandlisten] connection accepted from 127.0.0.1:57498 #20 (2 connections now open)
2014-12-27T15:52:05.848+0900 [initandlisten] connection accepted from 127.0.0.1:57499 #21 (3 connections now open)
2014-12-27T15:52:05.849+0900 [initandlisten] connection accepted from 127.0.0.1:57500 #22 (4 connections now open)
2014-12-27T15:52:05.849+0900 [initandlisten] connection accepted from 127.0.0.1:57501 #23 (5 connections now open)
2014-12-27T15:52:05.853+0900 [conn20] CMD: drop strongloop_test.Customer
2014-12-27T15:52:05.855+0900 [conn21] CMD: drop strongloop_test.Order
2014-12-27T15:52:05.856+0900 [conn22] CMD: drop strongloop_test.Review
2014-12-27T15:52:05.863+0900 [conn19] build index on: strongloop_test.Order properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "strongloop_test.Order" }
2014-12-27T15:52:05.863+0900 [conn19]    added index to empty collection
2014-12-27T15:52:05.864+0900 [conn20] build index on: strongloop_test.Customer properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "strongloop_test.Customer" }
2014-12-27T15:52:05.864+0900 [conn20]    added index to empty collection
2014-12-27T15:52:05.865+0900 [conn21] build index on: strongloop_test.Review properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "strongloop_test.Review" }
2014-12-27T15:52:05.865+0900 [conn21]    added index to empty collection


2014-12-27T15:54:04.810+0900 [initandlisten] connection accepted from 127.0.0.1:57979 #24 (6 connections now open)
2014-12-27T15:55:30.118+0900 [conn23] end connection 127.0.0.1:57501 (5 connections now open)
2014-12-27T15:55:30.118+0900 [conn22] end connection 127.0.0.1:57500 (5 connections now open)
2014-12-27T15:55:30.118+0900 [conn21] end connection 127.0.0.1:57499 (5 connections now open)
2014-12-27T15:55:30.118+0900 [conn20] end connection 127.0.0.1:57498 (5 connections now open)
2014-12-27T15:55:30.118+0900 [conn19] end connection 127.0.0.1:57497 (4 connections now open)

As a result of my debugging, this disconnection has been caused by 15 line of the following code. I think that 15 line dataSource.disconnect(); should be removed. After removing this line, everything runs well :)

module.exports = function(server) {
  var dataSource = server.dataSources.db;
  dataSource.automigrate('Review', function(er) {
    if (er) throw er;
    var Model = server.models.Review;
    //create sample data
    var count = reviews.length;
    reviews.forEach(function(review) {
      Model.create(review, function(er, result) {
        if (er) return;
        console.log('Record created:', result);
        count--;
        if (count === 0) {
          console.log('done');
          dataSource.disconnect();
        }
      });
    });
  });
};

yunchancho avatar Dec 27 '14 07:12 yunchancho