Jonathan has asked for the wisdom of the Perl Monks concerning the following question:

Below is a cut down (ie error checking removed etc) version of a script I'm working on. The idea is that I call stored procedures but use my own error handler to control execution
$dbh->{syb_err_handler} = \&err_handler;
There is no problem when the called procedure works OK but on error when I try to disconnect via the terminate function I get...
syb_db_disconnect(): ct_con_drop() failed
I've tried to clean up (calling finish on the statement handler) but can't get rid of the warning. Does any one have any other ideas?
#!/usr/local/bin/perl -w use strict; use DBI; use DBD::Sybase; my $dbh; my $sth; $dbh = DBI->connect( "DBI:Sybase:server=$ENV{'MY_SERVER'};database=$ENV{'MY_DB'}", $ENV{'MY_USER'}","$ENV{'MY_PASS'}", {AutoCommit => 1, PrintError => 0, RaiseError => 1}); # sql errors handler $dbh->{syb_err_handler} = \&err_handler; my $procedure_name = 'testproc'; $sth = $dbh->prepare("exec $procedure_name"); # Run the database procedure $sth->execute(); do { while (my $data = $sth->fetch()) { if ($sth->{syb_result_type} == CS_ROW_RESULT) { print @$data,"\n"; } } } while ($sth->{syb_more_results}); $dbh->disconnect; # Script ends sub err_handler { my($err, $sev, $state, $line, $server, $proc, $msg) = @_; if ($err == 0) { # This is ok print output print "$msg\n"; return 0; # This is not an error } else { # Problem encountered. my $errmsg = "Fatal error detected in procedure. $procedure_na +me\n"; $errmsg .= "Error number = $err\n"; $errmsg .= "Severity = $sev\n"; $errmsg .= "State = $state\n"; $errmsg .= "Line = $line\n"; $errmsg .= "Server = $server\n"; $errmsg .= "Procedure = $proc\n"; $errmsg .= "Message = $msg\n"; $sth->finish() if $sth; terminate($errmsg); } } sub terminate { my $errmsg = shift; print "$errmsg\n"; $sth->finish() if $sth; $dbh->disconnect if $dbh; exit 1; }

Replies are listed 'Best First'.
Re: DBI disconnection problems
by mpeppler (Vicar) on Oct 26, 2001 at 22:48 UTC
    Error callbacks are somewhat special beasts (and yeah, I know, it's not documented properly.)

    There are only certain database calls that you can use from within the callback - this is a limitation of OpenClient and of the TDS protocol, not of my code.

    The correct way to do this is to set a flag in the error handler, and to check the flag after the handler returns. Unfortunately that makes for somewhat ugly code.

    However, the ct_con_drop() failed warning is really not serious. You may be able to shut it up with a $SIG{__WARN__} = sub {}; in your terminate() sub.

    Michael

      Thanks mpeppler, I'll try that. (I had hoped you see my question)