Gossamer Forum
Home : General : Perl Programming :

disconnect and subs

Quote Reply
disconnect and subs
Do you have to disconnect before calling a sub or can you just disconnect at the end of the calling sub;

ie :
sub somthing {
if (condition) {
connect to mysql .......
perform query ........
if (condition) {
$dbh->disconnect;
&error_sub;
}else{
$dbh->disconnect;
&print_sub
}
else{
&error_sub;
}
}

or is this ok -

sub somthing {
if (condition) {
connect to mysql .......
perform query ........
if (condition) {
&error_sub;
}else{
&print_sub
}
$dbh->disconnect;
else{
&error_sub;
}
}

thanks

Bob
http://totallyfreeads.com




Quote Reply
Re: [lanerj] disconnect and subs In reply to
The way I always do it is disconnect at the end of the sub. I then have adisconnect within the error sub (which is called if something didnt workcorrectly), which then cleans up the connection if the script doesnt get to theend of the sub.



Hope that helps a bit Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [lanerj] disconnect and subs In reply to
Basically you just need to disconnect just before your script stops executing....either before you print html or in error routines...(generally)
Quote Reply
Re: [AndyNewby] disconnect and subs In reply to
The reason I asked was that some of GTs scripts just have the single disconnect at the end of the sub, which I couldn't realy understand as I was under the imperssion that perl evaluated scripts one line at a time, so it may never reach the disconnect in that case.
I just don't want to over do the disconnect calls if I don't have to.

Bob
http://totallyfreeads.com
Quote Reply
Re: [lanerj] disconnect and subs In reply to
Most servers also have a disconnection period (usually around 10-15 seconds). Basically, if you dont disconnect from the database, the server does it for you Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] disconnect and subs In reply to
Hnm! I never thought of putting a disconnect statement in my error_sub. Thanks for that tip, I'll add that in!

I wonder if die disconnects a database handle?

- wil
Quote Reply
Re: [Wil] disconnect and subs In reply to
Hi. I dont think it does. The die statement just stops any new queries in thescript being run (at least that is what the documentation i read about it saidTongue).

Glad that the error sub idea was helpful to you. It has kept my host happycompared to the people who end up not disconnecting theirdatabases...lol

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] disconnect and subs In reply to
Its a bit of a piss about with more complicated scripts using objects disconnecting all over the place.
Quote Reply
Re: [RedRum] disconnect and subs In reply to
  Thanks guys, I'll just disconnect before calling a sub unless that sub returns or unless the called sub also needs to perform a query, then I'll disconnect at the end of that sub.
Yep sure can get complicated.

Merry Xmas
Bob
http://totallyfreeads.com