[Melbourne-pm] DBI connect()'s, scope and disconnects

Brendon Oliver brendon.oliver at gmail.com
Thu Jul 26 21:23:20 PDT 2007


On Friday 27 July 2007 14:14:10 Leigh Sharpe wrote:
> Hi All,
> Can someone clear up a couple of things for me here?
> I have some code which does something like this:
>
> foreach(@an_array)
> {
>
>     my $dbh=DBI->connect($dsn);
>     # Do some other stuff here.
>     last if (some_condition == true);
>     $dbh->disconnect();
>
> }

If you have a currently-executing statement handle (eg. looping though query 
results), you may get some complaints from that (something along the lines of 
telling you that you should call finish() first), but generally destruction 
of a database object will close the connection (at least, that seems to be 
the case with DBD::Pg handles).

> Question is, if some_condition is true, the loop ends and $dbh goes out
> of scope. 

Why not just disconnect before the condition?

foreach(@an_array)
{
     my $dbh=DBI->connect($dsn);
     # Do some other stuff here.
     $dbh->disconnect();
     last if (some_condition == true);
}

seems to me to make more sense that way...

cheers,

- B>




-- 
BOFH excuse #434:

Please state the nature of the technical emergency

 14:18:39 up 2 days,  3:04,  2 users,  load average: 0.24, 0.19, 0.31



More information about the Melbourne-pm mailing list