在IISNODE上,nodejs应用程序无法连接到数据库回收或不活动的进程关闭

时间:2019-06-28 09:09:56

标签: node.js iisnode node-oracledb

由于某种原因,我不知道为什么节点应用程序在回收后无法重新连接数据库。可以在IISNode应用程序手动重新启动后完成连接。下面是我的server.js文件,该文件中没有发现任何错误;

const http = require('http');
var openHttpConnections = {};
var httpServer;
const dbOperationsWPool = require('./api/dbLib/dbOperationsWPool');

const compression = require('compression');
const helmet = require('helmet')
require('dotenv').config({
    path: __dirname + '\\mynode.env'
});
const dbPoolConfig = require("./dbPoolConfig");
const config = require('./config'); // get our config file
const bodyParser = require('body-parser');
var express = require('express'),
    cors = require('cors'),
    app = express();
// port = process.env.PORT || 4600;

const verifyToken = require('./api/lib/verifyToken');
app.use(helmet());
app.use(compression());
app.use(cors());
app.options('*', cors());
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());
app.set('superSecret', config.secret);

var authRoutes = require('./api/routes/authRoutes');
authRoutes(app); //register the route
var wsRoutes = require('./api/routes/wsRoutes');
wsRoutes(app);
// const wsVerifyToken = require('./api/lib/verifyWebServiceToken');
// app.use(wsVerifyToken);
app.use(verifyToken); // Yeri değiştirilmemeli. bu satırdan sonra oluşan routes token aranacaktır

const commonRoutes = require('./api/routes/commonRouter');
commonRoutes(app);
var izinRoutes = require('./api/routes/ikRoutes');
izinRoutes(app);

var satinalmaRoutes = require('./api/routes/satinalmaRoutes');
satinalmaRoutes(app);
var finansRoutes = require('./api/routes/finansRoutes');
finansRoutes(app);

var reportRoutes = require('./api/routes/reportRoutes');
reportRoutes(app);

app.use(handleError);
httpServer = http.Server(app);
app.use(function (req, res) {
    res.status(404).send({
        url: req.originalUrl + ' not found'
    })
});
httpServer.on('connection', function (conn) {
    var key = conn.remoteAddress + ':' + (conn.remotePort || '');
    openHttpConnections[key] = conn;
    conn.on('close', function () {
        delete openHttpConnections[key];
    });
});
httpServer.on('error', (err) => {
    console.log("Caught flash policy server socket error: ")
    console.log(err.stack)
});
/// create pool
dbOperationsWPool.createPool(dbPoolConfig).then(function () {
        // IS_NODE_PROD
        var port = 4900;
        if (process.env.IS_NODE_PROD === 'true' || process.env.IS_NODE_TEST === "true") {
            port = process.env.PORT;
        }

        httpServer.listen(port, function () {
            console.log('Webserver listening on localhost:' + port);
        });


    })
    .catch(function (err) {
        console.error('Error occurred creating database connection pool', err);
        console.log('Exiting process');
        process.exit(0);
    });

//app.listen(port);
//console.log('Onay RESTful API server started on: //' + port);



function handleError(err, req, res, next) {
    //console.error(err);
    res.status(500).send({
        error: 'Hata oluştu Erp yardım masasına başvurunuz'
    });
    shutdown(); //process would usually be restarted via something like https://github.com/foreverjs/forever
}

function shutdown() {
    console.log('Shutting down');
    dbOperationsWPool.terminatePool()
        .then(function () {
            console.log('node-oracledb connection pool terminated');
            console.log('Exiting process');
            process.exit(0);
        })
        .catch(function (err) {
            console.error('Error occurred while terminating node-oracledb connection pool', err);
            console.log('Exiting process');
            process.exit(0);
        });


    for (key in openHttpConnections) {
        openHttpConnections[key].destroy();
    }
}

唯一可疑的行是终止函数,该函数终止了该池。任何帮助表示赞赏。

0 个答案:

没有答案