> Замостите полную версию скрипта. С проверкой ping.#!/usr/bin/perl
package Testov_test;
use strict;
use warnings;
use utf8;
use open qw(:std :utf8);
use FindBin;
use lib $FindBin::Bin;
use DBI;
my $con_errstr = undef;
my $while_is_enabled = 1;
sub connect_db {
# connect db
my $dbh = DBI->connect("dbi:Pg:dbname=db_exp;host=192.168.170.1;port=5432",
"exp", "111",
{AutoCommit => 0, RaiseError => 0, PrintError => 0, pg_enable_utf8 => 1, ShowErrorStatement => 1, InactiveDestroy => 1});
unless ($dbh) {
$con_errstr = "cannot connect on database ($DBI::errstr)";
return undef;
} else {
return $dbh;
}
}
sub check_connect_db {
my $dbh = shift;
print "=============check conn db============\n";
my $conn_db = 0;
#print "ping=".$dbh->ping."\n";
unless ($dbh->ping) {
$dbh = undef;
$conn_db = 1;
}
while ($conn_db) {
print "lost connect to db, attempt restore the connection (interval 10sec)\n";
$dbh = connect_db;
if ($dbh) {
print "connection to db restored\n";
last;
}
print "sleep 10\n";
sleep 10;
}
print "======================================\n";
return $dbh;
}
my $dbh = connect_db;
if ($con_errstr) {
print "$con_errstr\n";
exit;
}
my $query = qq/INSERT INTO proba_connector (id, name, is_del) VALUES (?,?,?)/;
my $sth = $dbh->prepare($query);
while ($while_is_enabled) {
$dbh = check_connect_db($dbh);
print "===============INSERT=================\n";
my @values = (1, 'one', 1,
2, 'two' ,1,
3, 'tree', 1);
for (my $a=0; $a<3; $a++) {
my ($id, $name, $is_del) = splice @values, 0, 3;
# RaiseError => 0
my $rv = $sth->execute($id, $name, $is_del);
print "state=".$dbh->state."\n";
unless ($rv) {
print "ROLLBACK INSERT: ($DBI::errstr) ($DBI::err)\n";
$dbh->rollback;
last;
}
#print "sleep 10 transaction\n";
#sleep 10;
}
print "commit\n";
$dbh->commit;
print "======================================\n";
print "sleep 5\n";
}
скрипт отлично работает если базу положить
а вот если упадет туннель, то не работает :-(