DBI:断开连接 - 问题

发布于 2024-10-22 03:07:18 字数 190 浏览 4 评论 0原文

您会将部分 disconnect 代码称为线路噪声,还是将其保留原样?

use DBI;

my $dbh = DBI->connect ...
...
...
END {
    $dbh->disconnect or die $DBI::errstr if $dbh;
}

Would you call parts of the disconnect-code as line-noise or would you leave it as it is?

use DBI;

my $dbh = DBI->connect ...
...
...
END {
    $dbh->disconnect or die $DBI::errstr if $dbh;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叫思念不要吵 2024-10-29 03:07:18

如果您在执行完所有工作后退出程序,则并不严格需要显式断开与数据库的连接。但这是一个好主意,特别是在已经执行多个连接或将执行多个顺序连接的程序中。

有关详细信息,请参阅对 Perl DBI 进行编程

Explicit disconnection from the database is not strictly necessary if you are exiting from your program after you have performed all the work. But it is a good idea, especially in programs in which you have performed multiple connections or will be carrying out multiple sequential connections.

See Programming the Perl DBI for more info.

幸福不弃 2024-10-29 03:07:18

当心。如果禁用自动提交并且不提交,则可能会遇到一些有趣的情况,具体取决于您是否断开连接:

perl -le 'use DBI;my $h = DBI->connect("dbi:ODBC:test","test","test", {AutoCommit => 0, RaiseError=>1}); $h->do(q/insert into mje values(?, ?)/, undef, 1, "fred");'

由于 DESTROY 没有显式地断开 DBD::ODBC::db 句柄测试的断开连接()而发出回滚()。

请注意,由于没有显式断开连接,因此插入被回滚,并且我们收到了错误。

perl -le 'use DBI;my $h = DBI->connect("dbi:ODBC:test","test","test", {AutoCommit => 0, RaiseError=>1}); $h->do(q/insert into mje values(?, ?)/, undef, 1, "fred");$h->disconnect or die $DBI::errstr;'

在这里,即使未调用提交但行未进入数据库,似乎也没有任何问题。因此,断开连接掩盖了行未提交的事实。

Be careful. You can hit some interesting situations if you disable AutoCommit and don't commit depending on whether you disconnect:

perl -le 'use DBI;my $h = DBI->connect("dbi:ODBC:test","test","test", {AutoCommit => 0, RaiseError=>1}); $h->do(q/insert into mje values(?, ?)/, undef, 1, "fred");'

Issuing rollback() due to DESTROY without explicit disconnect() of DBD::ODBC::db handle test.

Note, because there was no explicit disconnect the insert was rolled back and we got an error.

perl -le 'use DBI;my $h = DBI->connect("dbi:ODBC:test","test","test", {AutoCommit => 0, RaiseError=>1}); $h->do(q/insert into mje values(?, ?)/, undef, 1, "fred");$h->disconnect or die $DBI::errstr;'

Here it appears there is nothing wrong even though commit was not called but the rows did not get into the database. So the disconnect masked the fact the rows were not committed.

望喜 2024-10-29 03:07:18

在剧本的最后,这可能并不重要。然而,无论如何添加它可能是值得的,只是为了在你自己之后明确地清理。它肯定不会造成伤害,而且我怀疑在某些情况下它肯定会有所帮助。

At the end of the script, it probably doesn't matter much. However it might be worth it to add it anyway just to explicitly clean up after yourself. It certainly won't hurt and I suspect there might be a few situations where it will definitely help.

蝶舞 2024-10-29 03:07:18

我认为这不是绝对必要的,但我发现它更整洁。

I don't think it is strictly necessary but I find it neater.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文