针对Express应用程序的NodeJS DB2连接池

在涉及到IBM DB2的NodeJS时,我看到了很less的在线文章。 我是NodeJS的新手,并且遇到了为我的web应用程序configuration连接池的问题。 我成功地运行节点应用程序在我的本地单连接,但不知道如何configuration连接池。 下面的代码是我如何为单一连接。

DBConnection JS:

module.exports = function(dbConnection) { var Pool = require('ibm_db').Pool; var pool = new Pool(); pool.open("MY_CONNECTION_STRING",function(err,connection){ //error handling logic ... dbConnection(connection); }); } 

应用程序监听器js:

 var express = require('express'); var app = express(); app.listen(8080,function(){ console.log("server started..); }); require('./DBConnection')(function(connection){ app.get('/getEmpId',function(req,res){ connection.query("MY_SQL_QUERY",function(error,results,fields){ //some other logic res.send(results[0]); //Closing connection connection.close(function(err2) { if(err2) console.log(err2); }); }); }); } 

需要您的build议来设置连接池,在并发用户正在访问并且在提供请求后closures连接时,我可以为每个请求使用一个连接。

您可以查看IBM node-ibm_db驱动程序提供的简要示例。 它有一个关于连接池的部分。 驱动程序重用节点odbc池,您需要调用Pool对象上的打开/closures调用。 以下是从node-ibm_db站点获取的示例:

 var Pool = require("ibm_db").Pool , pool = new Pool() , cn = "DATABASE=dbname;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=dbuser;PWD=xxx"; pool.open(cn, function (err, db) { if (err) { return console.log(err); } //db is now an open database connection and can be used like normal //if we run some queries with db.query(...) and then call db.close(); //a connection to `cn` will be re-opened silently behind the scense //and will be ready the next time we do `pool.open(cn)` });