DBI:断开连接 - 问题
您会将部分 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在执行完所有工作后退出程序,则并不严格需要显式断开与数据库的连接。但这是一个好主意,特别是在已经执行多个连接或将执行多个顺序连接的程序中。
有关详细信息,请参阅对 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.
当心。如果禁用自动提交并且不提交,则可能会遇到一些有趣的情况,具体取决于您是否断开连接:
由于 DESTROY 没有显式地断开 DBD::ODBC::db 句柄测试的断开连接()而发出回滚()。
请注意,由于没有显式断开连接,因此插入被回滚,并且我们收到了错误。
在这里,即使未调用提交但行未进入数据库,似乎也没有任何问题。因此,断开连接掩盖了行未提交的事实。
Be careful. You can hit some interesting situations if you disable AutoCommit and don't commit depending on whether you disconnect:
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.
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.
在剧本的最后,这可能并不重要。然而,无论如何添加它可能是值得的,只是为了在你自己之后明确地清理。它肯定不会造成伤害,而且我怀疑在某些情况下它肯定会有所帮助。
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.
我认为这不是绝对必要的,但我发现它更整洁。
I don't think it is strictly necessary but I find it neater.