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

Leigh Sharpe lsharpe at pacificwireless.com.au
Thu Jul 26 21:27:52 PDT 2007


 

>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).

I make sure all statement handles are finished at this point, so that's not a problem.
(This is actually using DBD::mysql)..

>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...


Except when you want to keep using the connection if the condition is false.

Regards,
             Leigh
 
Leigh Sharpe
Network Systems Engineer
Pacific Wireless
Ph +61 3 9584 8966
Mob 0408 009 502
Helpdesk 1300 300 616
email lsharpe at pacificwireless.com.au
web www.pacificwireless.com.au

-----Original Message-----
From: Brendon Oliver [mailto:brendon.oliver at gmail.com] 
Sent: Friday, 27 July 2007 2:23 PM
To: melbourne-pm
Subject: Re: [Melbourne-pm] DBI connect()'s, scope and disconnects

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

_______________________________________________
Melbourne-pm mailing list
Melbourne-pm at pm.org
http://mail.pm.org/mailman/listinfo/melbourne-pm




More information about the Melbourne-pm mailing list