I've been rewriting a database module to make a connection during it's constructor (previously each method opened and closed the connection). I figured I could also centralise closing connections by adding a DESTROY method. However, even though I'm checking the db handle still exists in DESTROY, when I try disconnecting I get these warnings

DB disconnect failed: $DBI::err=, $DBI::errstr= during global destruction.
Can't read $DBI::err, last handle unknown or destroyed during global destruction.
Can't read $DBI::errstr, last handle unknown or destroyed during global destruction.

I could just change it back, it was working fine before, but I'm curious as to why this fails while the handle still seems to exist. Anyone know? The relevant bits (I hope) are below:

sub new() { my $class = shift; my $self = {}; $self->{'database'} = "dbi:Sybase:database=$config{'DB_NAME'};serv +er=$config{'DB_SERVER'};hostname=$config{'DB_HOST'};port=$config{'DB_ +PORT'}"; $self->{'username'} = $config{'DB_USERNAME'}; $self->{'password'} = $config{'DB_PASSWORD'}; bless ( $self , $class ); $self->{'dbh'} = $self->connect(); return $self; } sub DESTROY { my $self = shift; if ( $self->{'dbh'} ) { $self->close(); } } sub connect() { my $self = shift; my $dbh = DBI->connect( $self->{'database'}, $self->{'username'}, +$self->{'password'} ); if ( !defined( $dbh ) ) { ... } else { return $dbh; } } sub close() { my $self = shift; my $dbh = shift || $self->{'dbh'}; unless ( $dbh->disconnect() ) { warn "DB disconnect failed: \$DBI::err=$DBI::err, \$DBI::errst +r=$DBI::errstr"; } }

In reply to using DESTROY to DBI disconnect by WAP_happens

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.