Gossamer Forum
Home : Products : Others : MySQLMan :

MySQL question

Quote Reply
MySQL question
Do I have to use $sth->finish within scripts to release resources after executing a query?



Quote Reply
Re: MySQL question In reply to
I believe that is used for disconnecting from the table you are connected to. It is a good idea to have that code in there. But I am sure Steven or Alex may be able to provide more info on that code.

Regards,

Eliot Lee
Quote Reply
Re: MySQL question In reply to
The reason I asked this question is, I have a forum script which uses MySQL, basically it works fine. But sometimes it becomes very unstable, from top list, I can see lots of alive forum_show.pl processes running in memory, like:

Code:
PID USER PRI NI SIZE RSS SHARE STAT LIB %CPU %MEM TIME COMMAND
........................................ 14.01 2.1 0 forum_show.pl
........................................ 5.22 1.2 0 forum_show.pl
........................................ 5.10 2.3 0 forum_show.pl
........................................ 4.88 1.1 0 forum_show.pl
........................................ 4.01 2.8 0 forum_show.pl
........................................ 2.01 3.3 0 forum_show.pl
........................................ 1.18 0.8 0 forum_show.pl
........................................ 0.37 2.2 0 forum_show.pl
All the forum_show.pl process are alive and running. This seems to happen regardless of how many users are accessing the board. The system slows down so much that.

Maybe my server provider limits the max. connection to database, so I am thinking of adding $sth->finish within scripts to release resource for other queries(?). But I am not sure if this can solve the problem, I am not MySQL expert. And If so, do I have to add $sth->finish after every query? Or only one at the end of every script (a script does several queries)?


Quote Reply
Re: MySQL question In reply to
You don't need to call ->finish on mysql if the variable goes out of scope and is destroyed. ie:

sub foo {
if (..) {
my $sth = ...
}
# sth is destroyed
}

if you are using globals, and your sth's don't get destroyed by going out of scope, you should call sth->finish.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: MySQL question In reply to
Mysql is good about destroying the handles, other databases are not.

One good thing is to always run under use strict, and check your error logs. You'll often be warned if an auto-destruct on the handle was called, and you'll know you need to add a manual release.

Especially with GT's code, using 'strict' and checking your error logs can provide a wealth of information, since they have good error/debug routines in most of their programs.

PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://postcards.com/FAQ


Quote Reply
Re: MySQL question In reply to
Thanks for your help, I did as Alex said, my script uses mod_perl (but the server doesn't have mod_perl installed) and always under "use strict;". I don't find ant error information from apache error log...so confused:(


Quote Reply
Re: MySQL question In reply to
When you notice it becomes unstable, run a `mysqladmin proc` to see what the queries are. It could just be you have a query that isn't indexed properly.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: MySQL question In reply to
Can I run this command "mysqladmin proc" from MySQLMan (SQL Monitor)? or any other way to get the results which "mysqladmin proc" outputs without using telnet. I can't telnet to server from my office:-(


Quote Reply
Re: MySQL question In reply to
Sure:

SHOW PROCESSLIST

will do the trick.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: MySQL question In reply to
Thanks Alex, I got the following result by using SHOW PROSESSLIST:

Id User Host db Command Time State Info
424880 hiu localhost users Sleep 4 NULL NULL
424881 hiu localhost users Sleep 4 NULL NULL
424882 hiu localhost users Sleep 4 NULL NULL
424883 hiu localhost users Sleep 4 NULL NULL
424884 hiu localhost users Sleep 4 NULL NULL
425508 hiu localhost users Sleep 4 NULL NULL
425510 hiu localhost users Sleep 4 NULL NULL
426063 hiu localhost users Sleep 14 NULL NULL
426064 hiu localhost users Sleep 14 NULL NULL
.......... lots of sleep commands .........
426091 hiu localhost users Query 0 NULL SHOW PROSESSLIST

what's the meaning of "sleep"?



Quote Reply
Re: MySQL question In reply to
Sounds like you have something not releasing connections. Sleep means it's not doing any queries, but is still conneceted. You will eventually run out of connections to mysql and you will start getting fatal errors from DBI.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: MySQL question In reply to
So how can I solve this? use $dbh->disconnect() to manually release connections?