turumkhan has asked for the wisdom of the Perl Monks concerning the following question: (database programming)

I am stuck with a very typical problem.... and need help badly..... The thing is i am using DBI to connect to a database and at the end of the script i disconnect form the database by
$db_handle->disconect();
but this disconnect happens at the end of script.. Now if something happens and my script is killed before it reaches the end of the script... how to i disconnect form the database. Like i do a CTRL + c on the script that kills the execution but doesnot disconnect from database.... so is there a way for me to disconnect from database when this happens.
thanks
- turum.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Is there a way disconnect from database
by mikeB (Friar) on Jun 28, 2001 at 23:04 UTC
    DBI will automatically close open connections at the end of a program, but will complain to let you know you forgot. This happens even with Ctrl-C, as the closing is handled by the connection's DESTROY method, which is invoked by Perl's garbage collection when the program terminates.

    A major problem with depending on the automatic close is the variance in handling of open transactions. Some DBD's will commit, while others rollback.

    Check out chapter 4 of Programming the Perl DBI (the ORA Cheetah book).

Re: Is there a way disconnect from database
by Mr. Muskrat (Canon) on Feb 10, 2004 at 04:42 UTC
    In most cases you can use:
    END { $db_handle->disconnect(); }
Re: Is there a way disconnect from database
by aijin (Monk) on Jun 28, 2001 at 22:48 UTC
    You should set up a signal handler.

    $SIG{INT} = \&some_subroutine;
    will run the subroutine on a Ctrl-C.

    $SIG{INT} = 'IGNORE';
    will ignore Ctrl-C and the program will keep running.