app does not reconnect to database after a while
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: package My; use 5.010; use Dancer ':syntax'; use Dancer::Plugin::Database; use Dancer::Plugin::EscapeHTML; use utf8::all; use My::Settings; use My::Tools; our ($my_settings, $dbh) = (); $dbh ||= database; $my_settings = get_settings('prod'); my $my_tools = My::Tools->new ( dbh => database, settings => $my_settings, ); 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? TIA! -- Wbr, Kõike hääd, Gunnar
On Mon, 11 Jun 2012 14:50:06 +0300 WK <wanradt@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@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
2012/6/11 David Precious <davidp@preshweb.co.uk>:
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.
Sounds logical, thank you! -- Wbr, Kõike hääd, Gunnar
2012/6/11 WK <wanradt@gmail.com>:
2012/6/11 David Precious <davidp@preshweb.co.uk>:
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.
Sounds logical, thank you!
Still i have other problem more here: i have $my_tools object, which should also have db handle. But this handle gets old too and needs to reconnect. How is best way to implement it, if i don't want to create new object on every request? -- Wbr, Kõike hääd, G
participants (2)
-
David Precious -
WK