使用Node JS在单个连接池中进行多个数据库连接

时间:2016-07-21 07:19:43

标签: node.js apigee

我正在尝试使用generic-pool包在Node js中创建数据库连接池。使用单个数据库,池工作正常,但我想在单个池中使用多个数据库。在这种情况下,我面临着一个问题。通用池不会为单个池中的数据库创建池。以下是我的代码。我正在使用trireme-jdbc进行连接池的JDBC连接和通用池。

var Pool = require('C:JCI/trireme-jdbc/node_modules/generic-pool').Pool;
var jdbc = require('C:/Program Files/nodejs/node_modules/trireme-jdbc');
var configOpenedge = require('C:/Program Files/nodejs/node_modules/trireme-jdbc/testconf/config-openedge.js');
var configPostgre = require('C:/Program Files/nodejs/node_modules/trireme-jdbc/testconf/config-postgre.js');

                var pool = new Pool({
                    name : 'Anil-JCI',
                    create : function(callback) {
                        var connOpenedge = new jdbc.Database({
                            url : configOpenedge.url,
                            properties : configOpenedge.properties,

                        });

                        var connPostgre = new jdbc.Database({
                            url : configPostgre.url,
                            properties : configPostgre.properties,

                        /*
                         * minConnections : 1, maxConnections : 2000, idleTimeout : 60
                         */
                        });

                        callback(null, connOpenedge);

                    },


                    destroy : function(client) {
                        client.end();
                    },
                    max : 10,
                    // optional. if you set this, make sure to drain() (see step 3)
                    min : 2,
                    // specifies how long a resource can stay idle in pool before being removed
                    idleTimeoutMillis : 30,
                    // if true, logs via console.log - can also be a function
                    log : true

                });

                console.log("connection created");

                pool.acquire(function(err, clientOpenedge, clientPostgre) {
                    if (err) {
                        throw err;
                    }

                    else {
                        clientOpenedge.execute(
                                'select * from "po" where "po-num" = ? and "vend-num" = ? ', [
                                        4322452, 4301170 ], function(err, result, rows) {

                                    // pool.release(client);

                                    console.log(err);
                                    rows.forEach(function(row) {
                                        console.log(row);
                                    });
                                    console.log("Openedge Data Printed...");
                                });
                        clientPostgre.execute("select * from employees ",
                                [ ], function(err, result) {

                                    // pool.release(client);

                                    console.log("we are in postgre");
                                    console.log(result);
                                    console.log("Postgre Data Printed...");
                                });
                    }
                });

0 个答案:

没有答案