Mojolicious connection to DB occasionally crashes

375 Views Asked by At

I run a productive app to manage publications of our research group. The app is written in mojolicious framework in perl. I use sqlite3 as a DB and Hypnotoad as an app server.

My problem is that the connection to the DB occasionally crashes (2 times per week) without giving a reasonable error message. The only thing that helps is restart of the Hypnotoad. Any ideas why this may happen?

Example of an error message:

[Fri Jan 16 08:43:09 2015] [error] Can't call method "execute" on an undefined value at /home/piotr/perl/publiste2/script/../lib/AdminApi/Core.pm line 525.

The code there looks as follows:

my $qry = "SELECT DISTINCT our_type FROM OurType_to_Type WHERE landing=1 ORDER BY our_type ASC";
my $sth = $dbh->prepare( $qry );  
$sth->execute(); # this is the line 525

The dbh variable is returned by helper:

DBI->connect('dbi:SQLite:dbname='.$config->{normal_db}, '', '') or die $DBI::errstr .". File is: ".$config->{normal_db};

Full code is available here: https://bitbucket.org/vikin9/hex64publicationlistmanager/src

3

There are 3 best solutions below

0
On BEST ANSWER

This error kept appearing twice a month. It was so annoying that I have rewritten the app to use MySQL and the proper DBI MySQL connector. Since then, no more crashes.

2
On

$dbh->prepare returns an undefined value. This can have several reasons. Try something similar to this:

$sth = $dbh->prepare($qry) or die $dbh->errstr;
1
On

I know it's been some time and maybe you have already figured out, but I thought I'll give it a try.

If you run it using hypnotoad, there's a known problem if you create the handlers before preforking: Database connection problem in preforking

Hope it helps