1
0
mirror of https://gitlab.com/depesz/explain.depesz.com.git synced 2024-11-24 08:42:27 +02:00

Automatically reconnect to DB if it failed.

Error on db error will appear, but on reload new connection will be
established (if it's possible).
This commit is contained in:
Hubert depesz Lubaczewski 2018-06-11 21:03:48 +02:00
parent 00d63257bb
commit b5e9191d56

View File

@ -9,7 +9,7 @@ use DBI;
use Date::Simple;
use English qw( -no_match_vars );
has dbh => undef;
has real_dbh => undef;
has connection_args => sub { [] };
has log => undef;
@ -52,17 +52,6 @@ sub register {
# register helper
$app->helper(
database => sub {
# not conected yet
unless ( $self->dbh ) {
# connect
$self->dbh( DBI->connect( @{ $self->connection_args } ) );
# raise error (for case, when "RaiseError" option is not set)
confess qq|Can't connect database| unless $self->dbh;
}
return $self;
}
);
@ -70,6 +59,21 @@ sub register {
return;
}
sub dbh {
my $self = shift;
if ( ( $self->real_dbh )
&& ( $self->real_dbh->state =~ m{^(08|S8|57)} ) )
{
$self->log->warn( 'DBH looks errored out, state ' . $self->real_dbh->state . ', reconnecting.' );
$self->real_dbh( undef );
}
$self->real_dbh( DBI->connect( @{ $self->connection_args } ) ) unless $self->real_dbh;
return $self->real_dbh;
}
sub user_login {
my $self = shift;
my ( $username, $password ) = @_;