В основном вы добавляете прослушиватель на объект соединения, а при разобщении или сбое снова создаете соединение.С небольшими изменениями вы можете принять этот подход и использовать слушателей, чтобы проверить, доступно ли соединение, если не соединиться снова.Может быть несколько причин, по которым закрытие соединения лучше обрабатывает исключения, проверьте, все ли еще подключено, и переподключите в случае ошибки.
Или вы можете попробовать этот NPM, это сделает переподключение для вас https://www.npmjs.com/package/oracledb-autoreconnect
Пинг мне, если вам нужно кальцификации.
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();