[Dancer-users] app does not reconnect to database after a while
David Precious
davidp at preshweb.co.uk
Mon Jun 11 14:09:53 CEST 2012
On Mon, 11 Jun 2012 14:50:06 +0300
WK <wanradt at gmail.com> wrote:
> Tere!
>
> I have a little intranet app (in development environment with Dancer's
> webserver), which fails to connect to database, when it is unused some
> hours. When on morning first user tries to get some page, he gets
> "before filter error":
>
> runtime error
> An error occured while executing the filter named before:
> DBD::mysql::db selectrow_array failed: MySQL server has gone away at
> /usr/local/lib/site_perl/My/Settings.pm line 136.
>
> Pointed line contains a query, which executes fine, when database
> connection is available (read: after restarting app).
>
> My app's main module logic is like this:
[...]
> our ($my_settings, $dbh) = ();
>
> $dbh ||= database;
[...]
> hook 'before' => sub {
> $my_settings = get_settings('prod');
> };
[...]
> sub get_settings {
> my $env = shift;
> my $my_settings = My::Settings->new (
> dbh => $dbh,
> env => $env,
> );
> return $my_settings->{settings};
> }
>
> In before hook i ask dynamic settings from database. Is there better
> way to do this. Or other solutions?
You're storing the handle given to you by the database() keyword in
$dbh - so even when the connection has gone away, you still try to use
it.
If you change the code above to call database() in the
My::Settings->new call instead of using $dbh (which could be old and
no longer connected), all should be fine (as the D::P::Database plugin
will ensure you get a working handle, reconnecting if needed).
Holding on to a database handle between requests as you're doing by
storing it in $dbh and reusing it prevents the plugin from doing its
job for you.
--
David Precious ("bigpresh") <davidp at preshweb.co.uk>
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan www.preshweb.co.uk/github
More information about the Dancer-users
mailing list