Make db->init() more sensible, config->db_connect is the only one handling the db connection (remote db->init from config->new)

This commit is contained in:
2025-07-06 15:23:44 +02:00
parent 2806ce4948
commit 058792486c
2 changed files with 3 additions and 6 deletions

View File

@ -17,8 +17,6 @@ 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 $self->{'config'}->get_dbh();
}
@ -31,18 +29,18 @@ sub connect {
my $db = $config->get_as_single_val('config','db');
my $dbh;
if($usr && $pwd && $host && $db) {
my $logstr = 'Connecting to 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=>0, AutoCommit => 1 }));
sleep($i);
$i++;
unless ($i % 10) {
$self->{'config'}->{'logger'}->log('Cannot connect to DB, reconnect retry '.$i);
$self->{'config'}->{'logger'}->log($logstr.'timed out, retry #'.$i);
}
}
$config->set_dbh($dbh);
#$self->{'config'}->{'logger'}->log("Sucsessfully connected to db"); FIXME add debug to config?
$self->{'config'}->{'logger'}->log($logstr.'done');
return $config->get_dbh();
} else {
$self->{'config'}->{'logger'}->log("Unable to connect to db, not enough parameters");

View File

@ -20,7 +20,6 @@ sub new {
$self->{'localcheck'} = My::parser::localcheck->new;
$self->{'block'} = My::parser::block->new($self->{'config'});
$self->{'db'} = My::parser::db->new($self->{'config'});
$self->{'db'}->init();
$self->{'config'}->set_db($self->{'db'});
return $self;
}