gpt4 book ai didi

node.js - Node : What happen if not close oracle connection

转载 作者:搜寻专家 更新时间:2023-10-31 23:47:53 26 4
gpt4 key购买 nike

我正在构建一个将连接到 oracle 数据库的 NodeJS 应用程序。

我想知道如果我不关闭连接并多次调用 fiftycent() 函数会发生什么情况?

var i=50;
function fiftycent() {
var oracledb = require('oracledb');

oracledb.getConnection(
{
user : "hr",
password : "welcome",
connectString : "localhost/XE"
},
function(err, connection)
{
if (err) {
console.error(err.message);
return;
}
connection.execute(
"SELECT department_id, department_name "
+ "FROM departments "
+ "WHERE department_id = :did",
[180],
function(err, result)
{
if (err) {
console.error(err.message);
return;
}
console.log(result.rows);
});
});
i=i-1;
if (i>0) fiftycent();
}

Node 服务器运行几天后,会不会出现内存故障之类的?

请注意,此示例的一部分来自 https://github.com/oracle/node-oracledb而且它们不包括

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

在他们的代码中。

预先感谢您的回答。

最佳答案

每次您调用 getConnection 时,它都会创建一个新的(完整的)连接到您的数据库。如果您不调用 release,它可能会导致您的应用程序内存泄漏,因为连接仍在分配中。也许根据您的数据库服务器设置,您可能会达到最大打开连接总数。

在这种情况下,最好将连接集中在连接池 中。调用 release 会将连接返回到池中,并使其可供其他调用使用。

所有示例都使用release 函数来释放连接。看看here

关于node.js - Node : What happen if not close oracle connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30440609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com