Disable auto-reconnect, and handle db/dbh centrally-ish

This commit is contained in:
2025-07-01 20:11:57 +02:00
parent 999bc6c8c8
commit 7b7083adca
6 changed files with 51 additions and 41 deletions

View File

@@ -2,7 +2,6 @@ package My::parser::db;
use strict;
use warnings;
use DBI;
#use Scalar::Util qw(weaken);
sub new {
my $class = shift;
@@ -19,8 +18,8 @@ sub new {
sub init {
my $self = shift;
my $parser = shift; ## not needed for this module as the db-connection is for all modules
$self->connect || die "Could not connect to db";
return 1;
$self->connect() || die "Could not connect to db";
return $self->{'config'}->get_dbh();
}
sub connect {
@@ -32,13 +31,19 @@ sub connect {
my $db = $config->get_as_single_val('config','db');
my $dbh;
if($usr && $pwd && $host && $db) {
my $i = 0;
$self->{'config'}->{'logger'}->log('Connecting to DB');
while(1) {
last if($dbh = DBI->connect("DBI:mysql:database=$db;host=$host",$usr,$pwd, { PrintError => 1, mysql_auto_reconnect=>1, AutoCommit => 1 }));
sleep(10);
last if($dbh = DBI->connect("DBI:mysql:database=$db;host=$host",$usr,$pwd, { PrintError => 1, mysql_auto_reconnect=>0, AutoCommit => 1 }));
sleep($i);
$i++;
unless ($i % 10) {
$self->{'config'}->{'logger'}->log('Cannot connect to DB, reconnect retry '.$i);
}
}
$config->set_dbh($dbh);
#$self->{'config'}->{'logger'}->log("Sucsessfully connected to db"); FIXME add debug to config?
return 1;
return $config->get_dbh();
} else {
$self->{'config'}->{'logger'}->log("Unable to connect to db, not enough parameters");
return 0;