Lost connection (fork, Log::Log4perl, MySQL)
I'm using the Mike Schilli's excellent CPAN module
Log::Log4perl for logging/debugging in my application.
Log::Log4perl is designed to provide a single
static/singleton logger object, but if I share a single
database connnection between processes, I get the
following error (as one might expect):
DBD::mysql::st execute failed: Lost connection to
MySQL server
during query at
/usr/lib/perl5/site_perl/5.6.1/Log/Log4perl/
Appender/DBI.pm line 214.
I've tried to add a simple reconnect method to
Log::Log4perl::Appender::DBI in order to handle calls
from forked
processes. Please see the attatched patch to DBI.pm.
Best regards,
Andreas [andreas1234567@hotmail.com]
PS! I failed to create a user account, thus anonymous
post. DS.
--
Logged In: NO
DBi.patch:
7a8,10
> my $pid = $$; # set pid
> my %prev_params;
>
14a18
> %prev_params = %p; # save parameters for reconnect
63a68,78
> sub _reconnect {
> my $self = shift;
>
> $self->{dbh}->{InactiveDestroy} = 1;
> $self->{dbh} = undef;
>
> # re-initialize db connection
> $self->_init(%prev_params);
>
> $self->{dbh}->{InactiveDestroy} = 0;
> }
78a94,99
> # reconnect if called by forked process
> if ($pid != $$) {
> $self->_reconnect();
> $pid = $$;
> }
>