Re: Perl DBI->Connect: how to detect a a lost connection




Ravi Malghan wrote:
Hi: I have a script which connects to a database when it starts up

$dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port;",
"$username", "$password", {AutoCommit => 1});

followed by a while loop which runs a query for this connection at 60 second
intervals. If the database goes down for some reason, I want the script to
try reconnecting to the database. How do I figure out within the while look
if the database connection is still valid. If tried using the $dbh variable
(if ($dbh)then connection is fin. else connection is bad). That doesn't seem
to work. How do I figure out if the $dbh connection has been lost within the
while loop?

I suggest you connect to the database every time around the loop instead of just
once before it. If you use the connect_cached method instead of connect then the
connection will be verified and used again if it is still valid.

HTH,

Rob



.



Relevant Pages

  • Re: ADO Connection Timeout
    ... so what happens when a connection failure forces one station to revert ... to a local database? ... Further, you *will* have contention issues, Jet does not support record ... to the central server, but you are willing to live with periods where it ...
    (microsoft.public.data.ado)
  • Re: ADO Connection Timeout
    ... When the first test is run, the results are stored in the central database. ... to the central server, but you are willing to live with periods where it ... i.e. a local database or even a text file. ... to function until the connection can be restored to the server. ...
    (microsoft.public.data.ado)
  • Re: ADO Connection Timeout
    ... to the central server, but you are willing to live with periods where it ... i.e. a local database or even a text file. ... to function until the connection can be restored to the server. ...
    (microsoft.public.data.ado)
  • Re: ADO Connection Timeout
    ... much rather write stored procedures and use server side cursors. ... local database, it is vital that I get the information to a central server. ... Once the connection is restored, ...
    (microsoft.public.data.ado)
  • Re: ESQL/C documentation nightmare
    ... documentation about explicit and implicit connections. ... CONNECTION connection statements for new applications of Version 6.0 ... the SQL database statements (such as ...
    (comp.databases.informix)