RE: Perl DBI->Connect: how to detect a a lost connection
- From: stewart.anderson@xxxxxxxxxxxxx (Stewart Anderson)
- Date: Mon, 21 Jul 2008 10:22:27 +0100
Ravi Malghan wrote:
Hi: I have a script which connects to a database when it starts upsecond
$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
intervals. If the database goes down for some reason, I want thescript to
try reconnecting to the database. How do I figure out within the whilelook
if the database connection is still valid. If tried using the $dbhvariable
(if ($dbh)then connection is fin. else connection is bad). Thatdoesn't seem
to work. How do I figure out if the $dbh connection has been lostwithin 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
I have seen in other implementations. using a simple "select
sysdate from dual" (which is very fast since it does not query any
table) type of check and trapping the response to see if it is
connected. But, that will take more instructions than simply
re-connecting, don't know how much of a performance thing it will
be versus the "select/check", Though if Rob suggests a re-connect,
he might already have been down that alley.
Stu
--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/
Information in this email including any attachments may be privileged, confidential and is intended exclusively for the addressee. The views expressed may not be official policy, but the personal views of the originator. If you have received it in error, please notify the sender by return e-mail and delete it from your system. You should not reproduce, distribute, store, retransmit, use or disclose its contents to anyone. Please note we reserve the right to monitor all e-mail communication through our internal and external networks. SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and are used under licence. British Sky Broadcasting Limited (Registration No. 2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited (Registration No. 2340150) are direct or indirect subsidiaries of British Sky Broadcasting Group plc (Registration No. 2247735). All of the companies mentioned in this paragraph are incorporated in England and Wales and share the same registered office at Grant Way, Isleworth, Middlesex TW7 5QD.
.
- References:
- Perl DBI->Connect: how to detect a a lost connection
- From: Ravi Malghan
- Re: Perl DBI->Connect: how to detect a a lost connection
- From: Rob Dixon
- Perl DBI->Connect: how to detect a a lost connection
- Prev by Date: Re: Arithmetic operation inside substitution
- Next by Date: Re: regular expression to exclude a phrase
- Previous by thread: Re: Perl DBI->Connect: how to detect a a lost connection
- Next by thread: how to convert data available from excel to hash?
- Index(es):
Relevant Pages
|