Nodejs刷新时创建了多个池

问题描述 投票:0回答:1

我有几个数据库,我正在使用node.js中的连接池。每次刷新页面时,我都会认为池已重新创建。我刷新页面3次和3次承诺已解决。我删除了几个数据库,只是为了使此处的读取变得更容易。enter image description here

并且如果我取消注释连接关闭行,我的应用程序将崩溃。我似乎无法弄清楚为什么

enter image description here

const config = require("../config/config");
const oracledb = require("oracledb");

var crm1connPromise = new Promise((resolve, reject) => {
    oracledb.createPool({
        user: config.crm1.user,
        password: config.crm1.password,
        connectString: config.crm1.connectString,
        poolAlias: config.crm1.poolAlias,
        poolMin: 0,
        poolMax: 10,
        poolTimeout: 300
    }, (error, pool) => {
        if (error) {
            reject(err);
        }
        resolve("CRM1 Promise resolved")
    });
});

var query2connPromise = new Promise((resolve, reject) => {
    oracledb.createPool({
        user: config.query2.user,
        password: config.query2.password,
        connectString: config.query2.connectString,
        poolAlias: config.query2.poolAlias,
        poolMin: 0,
        poolMax: 10,
        poolTimeout: 300
    }, (error, pool) => {
        if (error) {
            reject(err);
        }
        resolve("QUERY2 Promise resolved --------")
    });
});

var promiseArray = [crm1connPromise, crm2connPromise, crm3connPromise, crm4connPromise, csfp1connPromise, csfp2connPromise, csfp3connPromise, csfp4connPromise, cact1connPromise, cact2connPromise, cact3connPromise, cact4connPromise, cospconnPromise, cchnconnPromise, bbaseconnPromise, bcdrconnPromise, vcdbconnPromise, crptconnPromise, query2connPromise];

function getDBConnection (dbname) {

    return new Promise((resolve, reject) => {
        try {
            Promise.all(promiseArray).then((message) => {
                console.log(message);
                const pool = oracledb.getPool(dbname);
                pool.getConnection( (err, connection) => {
                    if (err) {
                        reject(err);
                        console.log(err);
                    }
                    resolve(connection);
                });
            });

        } catch (error) {
            reject(error);
        }

    });
}

module.exports.query = function(dbname, sql, bind = []){
    return new Promise ((resolve,reject) =>{
        var conn
        try {
            getDBConnection(dbname).then((connection) =>{
                connection.execute(sql,bind,(err,result)=>{
                    if (err){
                        reject(err);
                    }
                    resolve(result);
                })
                //connection.close(0);
            })
        } catch (error) {
            reject(error);
        }
    })
}
node.js oracle promise connection-pooling
1个回答
0
投票

您可以使用'Singleton'

请使用google'Singleton模式'和示例。

像这样:

dataBaseManager.js:

'use strict'

var Singleton = (function () {
    var instance;

    function createInstance() {
        var object = new dataBaseManager();
        return object;
    }

    return {
        getInstance: function () {
            if (!instance) {
                instance = createInstance();
            }
            return instance;
        }
    };
})();

function dataBaseManager() {
    this.connected = false;
    this.client = null;
    this.dataBase = null;

    //public methods
    this.connect = function () {
        try {

            your_database.connect({}, (err, client) => {
                if (err) {
                    this.connected = false;
                    this.client = null;
                    this.dataBase = null;
                    return;
                }
                this.connected = true;
                this.client = client;
                this.dataBase = client.db();
            });

        } catch (error) {

        }
    };
    this.disconnect = function () {
        try {
            if (this.client) {
                this.client.close();
                this.connected = false;
                this.client = null;
                this.dataBase = null;
            }

        } catch (error) {

        }
    }
}

module.exports = Singleton;

repository.js:

const dataBaseManager = require("./dataBaseManager").getInstance();

your_get_dample_data_from_data_base_func = function (data) {
  dataBaseManager.dataBase
    .find({})
    .toArray(function (err, result) {
      if (err) {
        return callback(err, null);
      }
      callback(null, result);
    });
};

index.js:

const dataBaseManager = require("./dataBaseManager").getInstance();

function connect() {
  dataBaseManager.connect();
}

function disconnect() {
  dataBaseManager.disconnect();
}
© www.soinside.com 2019 - 2024. All rights reserved.