Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

CGI::Application::Plugin::DBH connect error handling

by gman (Friar)
on Feb 10, 2011 at 20:24 UTC ( #887528=perlquestion: print w/replies, xml ) Need Help??
gman has asked for the wisdom of the Perl Monks concerning the following question:


I'm looking for a way to handle Can't connect error in DBH. Currently if the mysql DB is down I get the following:

DBI connect('database=xyz','xyz',...) failed: Can't connect to local +MySQL server through socket '/var/lib/mysql/mysql.sock' (111) at /usr +/local/lib/perl5/site_perl/5.10.1/CGI/Application/Plugin/ line +42

I looked through the docs and could not find a way. Since the handle is lazily created, I am assuming that CGI::Session is the first to make the DB call and trigger the error. There must be a proper way to handle this?

Thanks in advanced

Replies are listed 'Best First'.
Re: CGI::Application::Plugin::DBH connect error handling
by jaldhar (Vicar) on Feb 10, 2011 at 21:34 UTC

    IMO regardless of whether you use the plugin or plain DBI or whatever, you should wrap any database access calls in an eval{} (and RaiseError => 1 in dbh_config().) Too many different things can go wrong in a web app.


Re: CGI::Application::Plugin::DBH connect error handling
by Anonyrnous Monk (Hermit) on Feb 10, 2011 at 21:30 UTC

    In what way would you like to handle the error that the DB is down?

    As for the exception you're currently getting as a result, what DBI connect attributes have you configured?  The following snippet

    ... # use the parameters the user supplied else { require DBI; $self->{__DBH}{$name} = DBI->connect(@{ $self->{__DBH_ +CONFIG}{$name} }); # }

    apparently raises an exception, so my suspicion would be that you've somehow set RaiseError => 1.

    Maybe you could force a stack trace to get more detailed info on what exactly is triggering the connect, so you know what to put eval {} around?

Re: CGI::Application::Plugin::DBH connect error handling
by sundialsvc4 (Abbot) on Feb 10, 2011 at 22:37 UTC

    Depends entirely on how your site is built (e.g. is it mod_perl or not), but I like to insert code that constructs the DB handle at a known point ... that is to say, not “lazily” ... specifically so that I can gracefully deal with the proverbial “ka-ka” when (not if) “it happens.”   If the operation fails, it will throw an exception which I can catch, so I can respond by throwing up a “this site is temporarily unavailable” message instead of ... well ... throwing up.   :-}

Re: CGI::Application::Plugin::DBH connect error handling
by scorpio17 (Abbot) on Feb 11, 2011 at 22:36 UTC

    I put something like this in my instance script (and maybe in my application models, also):

    use CGI::Carp qw(fatalsToBrowser set_message); BEGIN { set_message( sub { my $msg = shift; # message for development server #print "<h3>Server Error!</h3><pre>$msg</pre>"; # message for production server print "<h3>Server Error!</h3><p>Please try again later</p>"; }); }

    Then I wrap all my database stuff with eval:

    eval { $db1 = DBI->connect( $CFG{'DB_DSN'}, $CFG{'DB_USER'}, $CFG{'DB_PASS'}, {RaiseError => 1, AutoCommit => 1, }, ); }; if ($@) { die "Database error: failed to connect.\n"; }

    This basically allows me to trap any DB related errors, and avoid sending too much information to the browser, unless I'm in "debug mode".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://887528]
Approved by Corion
[Discipulus]: good morning folks!

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-05-24 06:46 GMT
Find Nodes?
    Voting Booth?