ajeet@perl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks

There is a mystery kind of thing, that when i do some database operation in a signal handler, then after doing this operation, process causes to terminate.

Situation is something like this 1.> Suppose process is in some infinite loop 2.> I had written a signal handler for signal 'URG', something like this..

$SIG{'URG'} = sub { # # Update the current status of Host from Database A.> my $curStatus = $host->fetchResyncStatus(); B.> $host->setResyncStatus($curStatus); print "\n Received CLD in :: ". $host->getIP(); } ;
Now when i do only operation "B", with some value of $curStatus set, then after handling the signal, process resumes in infinite loop and continue working...But when i do operation "A" alone or with "B", which calls function below, it causes to terminate my process after completing the signal routine..
sub fetchResyncStatus { my $self = shift; my $hostname = $self->getIP(); my $status; # # Connect to database, If unable to do so, log # an error and return my $dbh = DBI->connect( "dbi:Oracle:<DBNAME>", "<UserName>", "<Password>" ) || do { $self->error(__LINE__.":". DBI::errstr."\n"); return 0; }; # # Sql query to retrieve host status from the database my $sql_query = "SELECT STATUS FROM NMS_CONFIG WHERE IPADDRESS='". +$hostname. "'"; # # Prepare SQL query, If unable to do so, log an error and # return my $sth = $dbh->prepare($sql_query) || do { $self->error(__LINE__.": Could not prepare statement: ".$db +h->errstr. "\n"); return 0; }; # # Execute the SQL query. If unable to o so, log an error and # return $sth->execute() || do { $self->error(__LINE__.": Could not execute statement ".$sth- +>errstr."\n"); }; # # sql_query returns a reference to a hash with NMS_CONFIG # column name as keys and the column entries as their # corresponding values. while (my $hash_ref = $sth->fetchrow_hashref() ) { $status = $hash_ref->{'STATUS'}; } # # Disconnect from the database. $dbh->disconnect(); # # Return the value of the recieved status #return $status; print "\n\nStatus in DB :: " .$status."\n"; return 'NORMAL';

after going through this function. i found that when i call DBI->connect, it causes the problem, However it connects to database, fetch desired thing, returns what is desired. but after doing so, terminate the process...

Please have some pointer on it...it's like murder mystery for me.... :(

Thanks in Advance

Replies are listed 'Best First'.
Re: Handling DB operations in signal handler
by almut (Canon) on Apr 10, 2010 at 09:18 UTC

    It's generally not a good idea to run long, complex stuff from a signal handler (even with Perl's "safe signals").  Best is to just set some flag in the signal handler that you check and act upon in the main loop.

      Thank You almut.... yeah, doing long operation was the problem...