0

私はmongooseとnodejs環境で役立つ他のものを使ってアプリケーション環境を作りました。そこで、各コンポーネントの永続化をセットアップするコンストラクターを定義しました。次に、再接続を何度も防ぐために、接続を永続的に保持することをお勧めします (そして、すでにいくつかの作業を書いています)。これは(現時点では)うまく機能していますが、答えが見つからないことが2つあります...

1)接続が失われたようなエラーを処理できますか(DBサーバーがダウンするなど)

db.on('error', function(error) {
    console.log('MONGO: ERROR');
    // evt is not fired when DB server goes down
});

db.once('disconnect', function callback() {
console.log('MONGO: DISCONNECTED');
    // evt is also not fired when DB server goes down
});

2) ユーザー要求ではなく、アプリケーションの起動時に行われる永続的な接続を使用することはまったく良い考えですか?

すべてのコンポーネントは、接続を確立し、modelName、schemaDefinition などを開始する Persistence Layer から拡張されます。

Persistence = function(modelName, schemaDefinition, config) {

this.config = config;

if ( typeof (modelName) === 'string' && schemaDefinition instanceof Object) {
    this.mongoose = require('mongoose');

    if (this.mongoose.connection.readyState == this.config.settings.DATABASE.disconnected) {
        var con = this.mongoose.connect('mongodb://' + this.config.settings.DATABASE.host + '/' + this.config.settings.DATABASE.collection);
    }

        this.modelName = modelName;
    this.schemaDefinition = schemaDefinition;

    var db = this.mongoose.connection;

    db.on('error', function(error) {
    console.log('MONGO: FERROR');
    });

    db.once('open', function callback() {
        console.log('MONGO: CONNECTED');
    });

    db.once('disconnect', function callback() {
    console.log('MONGO: CONNECTION lost');
    });
      }
 };
4

0 に答える 0