Hi All,
I am trying to work on a piece of code which opens a connection and keeps
that connection open for an long time days if not months.
In order to see if the connection is open I am wanting to use the ping
command to see if the connection is still open.
I am using the code listed below to test to see if this is working.
It appears to be working ok, when I start the script running it connects to
the mysql server and prints out the correct information showing that the
connection is there and that data is been retrieved from the database. When
I stop the mysql server it returns the correct data saying that the ping
failed.
The question I have is that when I started mysql server again the script
prints out the information saying that it is connected to the mysql server
and that it is retrieving data.
I would have thought that once the connection has been broken that would be
it and I would have to issue the connect statement again to connect to the
DB, but it appears that the connection to the database is established
automatically.
Is this to be expected?
Thanks,
Adam
Script:
use DBI;
my $database = "db";
my $hostname = "localhost";
my $user = "user";
my $password = "pass";
my $dsn = "DBI:mysql:database=$database;host=$hostname";
my $dbh = DBI->connect($dsn, $user, $password,{ RaiseError => 1,
AutoCommit => 1 }) or die "Can't connect to $dsn: $DBI::errstr";
while(1)
{
if(eval{ $dbh->ping})
{
print "worked";
$sql = "select * from Queued";
$sth = $dbh->prepare($sql);
$rc = $sth->execute or die "Can't execute statement: $DBI::errstr";
if($sth->rows == 1)
{
print "data returned";
}
else
{
print "no data returned";
}
}
else
{
print "failed";
}
}
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.557 / Virus Database: 349 - Release Date: 01/01/2004