writing a bulletproof perl daemon with persistant mysql connection

Posted on 2006-04-03
Last Modified: 2008-02-01
Time is of the essence while sending "report" data back to a mysql database over the internet.  My goal is to write a bulletproof daemon that handles a constant connection to this database.

My server daemon pre-forks "worker" children to do time consuming work, and pre-forks a "reporting" child to handle sending reports from the "workers" back to the database.

In the "reporting" child I have a 60 second alarm signal inside of an eval{} statement that encloses the mysql database work.  No database work should take longer than a few seconds.  After a day or two of uptime, the "reporting" child hangs for approx 1 hour or more where nothing takes place and then the "reporting" child dies because of the alarm signal.  The eval{} statement is unable to trap the die().

I have 3 of these daemons running on different machines across the internet, and while 1 is hanging, 2 of the 3 are still able to report to the mysql database just fine.

Here is my code:

my $dbh;
eval {
      my $dsn = "DBI:$driver:database=$database;host=$hostname;mysql_connect_timeout=10";
      $dbh = DBI->connect($dsn, $username, $password, { RaiseError => 1, PrintError => 0, AutoCommit => 1 });
      $dbh->{mysql_auto_reconnect} = 1;
if( $@ ) {
      goto RECONNECT;

while( 1 ) {
      eval {
            #      Setup ALRM handler signal
            local $SIG{ALRM} = sub { die "timeout\n" };
            alarm( 60 );

            $dbh->do("INSERT INTO table_name VALUES ( data, ... )");

            alarm( 0 );
      if( $@ ) {
            if( $@ =~ /timeout/ ) {
                  print "Timed out\n";
            } else {
                  alarm( 0 );

When bad things happen, how would you go about setting up a daemon that has the best possible connection with the mysql server?
Question by:rivusglobal
    1 Comment
    LVL 22

    Accepted Solution

    Maybe you should just open a connection when you need to send a report in, then close it right after.  That way you don't have to depend on auto-reconnection behavior.  If you can deal with a 60-second lag before your alarm, the second or two it takes to connect every time should be trivial.


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now