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

Hi, I keep getting this error- DBI handle cleared whilst still active Any idea? Thanks.

Replies are listed 'Best First'.
Re: DBI ERROR
by Asim (Hermit) on Apr 13, 2001 at 01:24 UTC

    You need to do a $result = $statement_handle->finish on your statement handle, after you've finished fetching the data from it. The statement handle is what comes back from a prepare call. Look at your DBI documentation for more info -- I think the DBI FAQ covers this exact issue, in fact.

    ----Asim, known to some as Woodrow.

      I am already using finish for each my statement handler.
        Obligatory request for code sample.

        For example, your program may have died and during cleanup, $dbh->disconnect would have caused the above message (since you didn't call finish if the program died before the statement was executed).

        I've seen this myself, and unless it's actually impeding further lines from executing, it's mostly harmless. Of course it does go against programmer nature to allow any nuisance messages that you could stop. I do believe that disconnect or a second statement handle is coming into play before your first finish.

        Obligatory request for system info:

        • Database (version)
        • DBI/DBD (version)

        We may be able to see it once we know more.

        ALL HAIL BRAK!!!

Re: DBI ERROR
by runrig (Abbot) on Apr 13, 2001 at 02:21 UTC
    Can you post a bit of code? Can you add some debug statements (with $| = 1) or run it in the debugger to determine exactly where the error (warning more likely) occurs?

    Good Luck!

      my ($self, @cursors) = @_; my $cursor; foreach $cursor (@cursors) { if (exists($self->{CURSORS}{$cursor})) { # eval { my $ref = $self->{CURSORS}{$cursor}; delete($self->{CURSORS}{$cursor}); $ref->finish; } } } } if ($self->{CONNECTION} && !$self->{CONNECTION}->{InactiveDestro +y}) { $self->{CONNECTION}->disconnect;
      I am checking if there is any open cursor. Hope it helps. DBI Version is 1.06 and perl version is 5.0.3
        Tim Bunce sent me an e-mail earlier this week where he pointed out that it's very rare that you need to call finish() explicitly. If you're querying a large result set and only fetch a couple of rows, you could call it to free up memory.

        I don't have the DBI book with me, or I'd quote chapter and verse, but I know there's something about it in the DBI manpage. You can probably get by without it here, unless you're doing something exotic. (You might also like prepare_cached()).