简体   繁体   English

如何确定Perl DBI数据库处理程序的连接状态

[英]How to determine connection state of Perl DBI database handler

How to determine connection state of Perl DBI database handler(is connection opend)? 如何确定Perl DBI数据库处理程序的连接状态(是连接opend)? Something like .NET SqlConnection.State == Open. 像.NET SqlConnection.State == Open这样的东西。 May be something like 可能是这样的

defined($dbh->do("some nop sql"))

but can't find sql nop statement to use. 但是找不到sql nop语句来使用。

You can ask you database handle if it is connected by calling 如果通过调用连接,您可以询问数据库句柄

$dbh->ping();

Some DB Drivers don't implement ping but DBD::mysql does. 有些DB驱动程序没有实现ping但DBD :: mysql没有。 An alternative is to run an empty select like select 1 for MySQL. 另一种方法是运行一个空选择,例如select 1用于MySQL。 I'm assuming MySQL since that is how your question is tagged. 我正在假设MySQL,因为这是你的问题被标记的方式。 Other databases will have slightly different answers. 其他数据库的答案会略有不同。

There are two parts to this answer. 这个答案分为两部分。

The first answer is the {Active} field. 第一个答案是{Active}字段。 perldoc DBI says: perldoc DBI说:

ATTRIBUTES COMMON TO ALL HANDLES 所有手柄都有共同点

These attributes are common to all types of DBI handles. 这些属性对于所有类型的DBI句柄都是通用的。 [...] [...]

"Active" (boolean, read‐only) “活动”(布尔值,只读)

The "Active" attribute is true if the handle object is "active". 如果句柄对象为“活动”,则“活动”属性为true。 This is rarely used in applications. 这在应用程序中很少使用。 The exact meaning of active is somewhat vague at the moment. 活跃的确切含义目前有些模糊。 For a database handle it typically means that the handle is connected to a database ("$dbh−>disconnect" sets "Active" off). 对于数据库句柄,它通常意味着句柄连接到数据库(“$ dbh-> disconnect”将“Active”设置为off)。

That's probably what you want to check. 这可能就是你要检查的内容。

The second answer is that, while you can call ping() , or check the result of SELECT 1 , there's not much point. 第二个答案是,虽然你可以调用ping() ,或者检查SELECT 1的结果,但没有多大意义。 That will indeed tell you if the database handle is connected at the time of that check. 这确实会告诉您在检查时是否连接了数据库句柄。 But what you really want to know is whether the database handle is connected when you do what you're about to do next, right? 但是你真正想知道的是,当你做下一个即将做的事情时,数据库句柄是否已连接,对吧? And there's always a chance that the connection will fail between your check and whatever it is you actually want to do. 并且总是有可能在您的支票和您实际想要做的事情之间连接失败。 So a true result from either of those isn't a guarantee of anything. 因此,其中任何一个的真实结果都不是任何保证。

If you're doing status monitoring, then a ping() or SELECT 1 will do fine. 如果你正在进行状态监控,那么ping()SELECT 1就可以了。 In an application, though, don't check a dbh's validity before doing something. 但是,在应用程序中,在执行某些操作之前不要检查dbh的有效性。 Just connect, and use the dbh you get back, and do proper error-checking at every step. 只需连接,然后使用你回来的dbh,并在每一步都进行适当的错误检查。 There's no substitute for correctly checking for errors. 正确检查错误是无可替代的。

ping方法 - 虽然它的作用是依赖于数据库驱动程序。

there's also $dbh->state() 还有$dbh->state()

but yeah proper error-checking at every call is more certain. 但是,在每次通话时正确的错误检查都更加确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM