I am using DBI perl to connect with Sybase dataserver. My process does the following in loop that runs throughout the day
Till end of day, do {
$sth = $dbh->prepare
execute query
someAnotherPerlFunction()
someAnotherPerlFunctionOne()
}
someAnotherPerlFunction()
{
$sth = $dbh->prepare (select)
execute query
}
someAnotherPerlFunctionOne()
{
my $sth = undef;
$sth = $dbh->prepare (update)
execute query;
undef $sth;
}
Now, given that this will run throughout the day, what are some of the things that I need to keep in mind in terms of resource cleanup.
Currently, I am doing undef $sth
after each function, as shown in someAnotherPerlFunctionOne. Is that necessary?
Perl将为您进行清理,但是最好将db句柄传递给函数,而不是每次都重新创建它并立即销毁它。
There are a few things to look out for, but using undef
isn't one of them
If your process is running all day then you should keep checking that the connection is still live. You can do that using $dbh->ping
You should prepare
each statement only once. After that you may execute it many times with different bound parameters
If you ever leave a SELECT
statement handle without fetching all of its data then you should call $sth->finish
to reset it
Your pseudo-code is very limited, and doesn't explain much. In particular I don't understand how a config file (a data file) can create a database handle for you. Because you need to check whether a database connection is still active, you also need to be able to reconnect whenever necessary. If that is done in a "config file" then I think you need to readdress your design
Here's a rather more Perlish pseudo-code example of how I think it should work. Every time around the until ( $end_of_day )
loop the first thing is to check whether the database handle is still valid. If not then there is the point when you should establish a connection and prepare
all of your statement handles
After that, it is best to pass both the database handle and the statement handle to any subroutines. You will notice that, once the prepare
has been done for each statement handle, the two subroutines collapse to the same code
This needs filling out a lot, but I know almost nothing about your application. I hope it helps
use DBI;
my $dbh;
until ( $end_of_day ) {
my ($sth, $select, $update);
until ( $dbh and $dbh->ping ) {
$dbh = DBI->connect(...);
if ( $dbh ) {
$select = $dbh->prepare('SELECT ...');
$update = $dbh->prepare('UPDATE ...');
$sth = $dbh->prepare('SOME SQL');
last;
}
sleep 10; # Wait before retrying
}
$sth->execute();
someAnotherPerlFunction($dbh, $select);
someAnotherPerlFunctionOne($dbh, $update);
}
someAnotherPerlFunction {
my ($dbh, $sth) = @_;
$sth->execute;
}
someAnotherPerlFunctionOne {
my ($dbh, $sth) = @_;
$sth->execute;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.