How to determine the connection status of a Perl DBI database handler

How to determine the connection status of a Perl DBI database handler (is there an opend connection)? Something like .NET SqlConnection.State == Open. Maybe something like

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

but cannot find sql nop statement to use.

+3
source share
4 answers

You can set your database descriptor if it is connected by calling

$dbh->ping();

Some DB drivers do not implement ping, but DBD :: mysql does. An alternative is to run an empty type selector select 1for MySQL. I assume MySQL since your question is tagged. Other databases will have slightly different answers.

+12
source

.

- {Active}. perldoc DBI :

DBI-. [...]

"" (, )

     

"Active" ,    handle .    .      .    ,      ( "$ dbh- > disconnect" "Active"   ).

, , , .

: ping() SELECT 1, . , . , , , , , , ? , , . , -.

, ping() SELECT 1 . dbh, - . dbh, , . .

+4

The ping method - although what it does will be dependent on the database driver.

+1
source

there also $dbh->state()

but correct error checking on every call is more reliable.

+1
source

Source: https://habr.com/ru/post/1758061/


All Articles