【发布时间】:2017-06-24 07:36:44
【问题描述】:
我正在使用 DBI perl 连接 Sybase 数据服务器。我的流程在全天运行的循环中执行以下操作
Till end of day, do {
$sth = $dbh->prepare
execute query
someAnotherPerlFunction()
someAnotherPerlFunctionOne()
}
someAnotherPerlFunction()
{
$sth = $dbh->prepare (select)
execute query
}
someAnotherPerlFunctionOne()
{
my $sth = undef;
$sth = $dbh->prepare (update)
execute query;
undef $sth;
}
现在,鉴于这将全天运行,在资源清理方面我需要牢记哪些事项。
目前,我在每个函数之后都在做undef $sth,如 someAnotherPerlFunctionOne 所示。那有必要吗?
【问题讨论】:
-
someAnotherPerlFunctionOne 中的 undef 是不需要的。一旦超出范围,在函数内部定义的变量就是 undef。
-
更重要的是数据库的句柄($dbh)。最终需要提交,如果在您的代码中关闭了 AutoCommit,或者如果您不保持持久连接,您可能需要调用 $dbh->disconnect。
-
你为什么要问这个?你的程序有问题吗?如果是,请说明问题,否则保持原样。
-
@JohnDoe:我们通常通过测试来做到这一点。
-
@user3606329:"如果您的代码中关闭了 AutoCommit ...您可能想调用
$dbh->disconnect" 断开连接似乎有点奇怪,这将丢弃所做的所有更改。提交事务的方式是调用$dbh->commit,但AutoCommit默认是on,所以可以合理地假设OP没有改变它。
标签: perl sybase dbi resource-cleanup