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

I understand this is probably bad practice; however, I'm asking for educational reasons, not practical.

Say I do the following:
my $dbh = $self->mysql_connect( $mdatabase, $mhostname, $muser, $m +pass );

in a function called say, lookup.. NEVER disconnecting my previous connection

What would happen if I called lookup() 100 times in a row with different parameters. Would we have 100 database connections in memory? Or would each one get closed and a new one established.

My thoughts are that since there is no handle to the memory, then it'll probably just run some sort of cleanup on it. However, I'm curious to know for sure.

Mutt

Replies are listed 'Best First'.
Re: DBI Mysql Question
by davorg (Chancellor) on Nov 14, 2006 at 15:41 UTC

    Firstly, you don't seem to be using the DBI in that code, so your title looks inaccurate. Of course, you might be using DBI calls in the "mysql_connect" function, but we don't know.

    But to answer your question, the answer is that it depends on the rest of your code. What happens to the $dbh object? Does it go out of scope at the end of the function? Or is it returned from the function and stored in a different variable? Without seeing more of the structure of your code it's impossible to give any firm answers.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: DBI Mysql Question
by mutt (Novice) on Nov 14, 2006 at 15:35 UTC
    See, I didn't RTFM! I did a google search, and here's my answer:

    What happens if you don't explicitly disconnect? Since DBI handles are references to Perl objects, Perl's own garbage collector will move in and sweep up any object trash you leave lying around. It does that by calling the object's DESTROY method when there are no longer any references to the object held by your script, or when Perl is exiting.

    The DESTROY method for a database handle will call disconnect() for you, if you've left the handle connected, in order to disconnect cleanly from the database. But it will complain about having to do so by issuing a warning:

    Database handle destroyed without explicit disconnect.

    A major caveat with the disconnect() method regards its behavior towards automatically committing transactions at disconnection. For example, if a program has updated data but has not called commit() or rollback() before calling disconnect(), the action taken by different database systems varies. Oracle will automatically commit the modifications, whereas Informix may not. To deal with this, the DESTROY method has to call rollback() before disconnect() if AutoCommit is not enabled. In Chapter 6, we'll discuss the effect of disconnect() and DESTROY on transactions in more detail.

      Might be nice to give a link to wherever you're quoting from. Just in case someone else comes along with the same question.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: DBI Mysql Question
by Anonymous Monk on Nov 14, 2006 at 16:40 UTC
    Well, you have now read the manual (or at least, a manual). But even if you hadn't read the manual, it would still have been quite easy to try and find out, wouldn't it?