Changeset 32581


Ignore:
Timestamp:
2018-11-07T19:04:30+13:00 (5 years ago)
Author:
ak19
Message:

Adding sigint, sigterm and sigkill handlers to gssql, which all call finish_signal_handler which does a die(). When perl executes a die statement, it always calls the object destructor. Added an destructor sub DESTRUCT to gssql to close the connection to the db, on GLOBAL DESTRUCT PHASE, if singleton db_handle is not undef. I'm thinking that this is where autocommit rollbacks will need to be added as well.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/gssql.pm

    r32580 r32581  
    3333use DBI; # the central package for this module used by GreenstoneSQL Plugout and Plugin
    3434
     35#$SIG{INT}  = sub { die "Caught a sigint $!" };
     36#$SIG{TERM}  = sub { die "Caught a sigterm $!" };
     37#$SIG{KILL}  = sub { die "Caught a sigkill $!" };
     38
     39
     40$SIG{INT}  = \&finish_signal_handler;
     41$SIG{TERM}  = \&finish_signal_handler;
     42$SIG{KILL}  = \&finish_signal_handler;
     43
     44sub finish_signal_handler {
     45    my ($sig) = @_; # one of INT|KILL|TERM
     46    die "Caught a $sig signal $!"; # will call destructor
     47}
    3548##############################
    3649
     
    94107}
    95108
    96 
     109# On die(), an object's destructor is called.
     110# See https://www.perl.com/article/37/2013/8/18/Catch-and-Handle-Signals-in-Perl/
     111# We want to ensure we've closed the db connection in such cases.
     112# "It’s common to call die when handling SIGINT and SIGTERM. die is useful because it will ensure that Perl stops correctly: for example Perl will execute a destructor method if present when die is called, but the destructor method will not be called if a SIGINT or SIGTERM is received and no signal handler calls die."
     113# https://perldoc.perl.org/perlobj.html#Destructors
     114sub DESTROY {
     115    my $self = shift;
     116
     117    if (${^GLOBAL_PHASE} eq 'DESTRUCT') {
     118    if ($_dbh_instance) {
     119        print STDERR "XXXXXXXX Global Destruct: Disconnecting from database\n";
     120        $_dbh_instance->disconnect or warn $_dbh_instance->errstr;
     121        $_dbh_instance = undef;
     122        $ref_count = 0;
     123    }
     124    return;
     125    }
     126}
    97127
    98128#################################
Note: See TracChangeset for help on using the changeset viewer.