Node.js 如何避免与ORACLEDB的连接中断?Nodejs

Node.js 如何避免与ORACLEDB的连接中断?Nodejs,node.js,node-oracledb,Node.js,Node Oracledb,我有这个数据库连接。在注释所在的函数中,RESTAPI有一个数据更新周期。数据会被更新,但当Oracle数据库中的数据被更新时,连接可能会失败,之后所有后续更新的数据都将得到未定义的。如何正确连接到数据库以避免出现故障 oracledb.getConnection( { user: db.user, password: db.password, connectString: db.connectString }, connE

我有这个数据库连接。在注释所在的函数中,RESTAPI有一个数据更新周期。数据会被更新,但当Oracle数据库中的数据被更新时,连接可能会失败,之后所有后续更新的数据都将得到未定义的
。如何正确连接到数据库以避免出现故障

oracledb.getConnection(
    {
        user: db.user,
        password: db.password,
        connectString: db.connectString
    },
    connExecute
);

function connExecute(err, connection) {
    if (err) {
        console.error(err.message);
        return;
    }
    sql = `SELECT * FROM db.test`;
    connection.execute(sql, {}, { outFormat: oracledb.OBJECT },
        function (err, db) {
            if (err) {
                console.error(err.message);
                connRelease(connection);
                return;
            }
            // data update loop

            connRelease(connection);
        });
}

function connRelease(connection) {
    connection.close(
        function (err) {
            if (err) {
                console.error(err.message);
            }
        });
}

大多数情况下,在连接对象上添加侦听器,在分离或失败时再次创建连接。只要稍作修改,您就可以采用这种方法,并使用侦听器检查连接是否可用(如果不再连接)。可能有几个原因导致连接关闭。更好地处理异常,检查是否仍然连接,并在出现错误时重新连接

或者你可以试试这个NPM,它会帮你重新连接

如果你需要钙化,打电话给我

var dbConfig = {
    host: '----',
    user: '----',
    password: '----',
    database: '----',
    port: ----
};

var connection;
function handleDisconnect() {
connection = <obj>.getConnection(dbConfig);  
// Recreate the connection, since the old one cannot be reused.
connection.connect( function onConnect(err) {   
// The server is either down
    if (err) {                                  
// or restarting (takes a while sometimes).
        console.log('error when connecting to db:', err);
        setTimeout(handleDisconnect, 10000);
// We introduce a delay before attempting to reconnect,
    }                                           


 // to avoid a hot loop, and to allow our node script to
    });                                        
     // process asynchronous requests in the meantime.

     // If you're also serving http, display a 503 error.
        connection.on('error', function onError(err) {
        console.log('db error', err);
        if (err.code == 'PROTOCOL_CONNECTION_LOST') {
            handleDisconnect();                       
      // lost due to either server restart, or a
        } else {                                  
          // connnection idle timeout (the wait_timeout
            throw err;                          
            // server variable configures this)
            }
        });
    }
    handleDisconnect();
var dbConfig={
主机:'---',
用户:'---',
密码:'---',
数据库:'---',
端口:----
};
无功连接;
函数handleDisconnect(){
connection=.getConnection(dbConfig);
//重新创建连接,因为旧连接无法重复使用。
connection.connect(函数onConnect(err){
//服务器已关闭
如果(错误){
//或重新启动(有时需要一段时间)。
log('连接到数据库时出错:',错误);
设置超时(handleDisconnect,10000);
//我们在尝试重新连接之前引入延迟,
}                                           
//避免热循环,并允许我们的节点脚本
});                                        
//同时处理异步请求。
//如果您还提供http服务,则显示503错误。
connection.on('error',函数onError(err){
console.log('db error',err);
如果(err.code=='PROTOCOL\u CONNECTION\u LOST'){
handleDisconnect();
//由于服务器重新启动或故障而丢失
}否则{
//连接空闲超时(等待超时
犯错误;
//服务器变量(配置此变量)
}
});
}
handleDisconnect();

您应该使用连接池。连接池必须检测有问题的连接,并透明地创建新连接。有关更多详细信息,请参阅本系列“创建REST API”:

请记住,问题仍然可能发生,因此您必须根据应用程序的需要处理错误