简体   繁体   English

在DBI Perl中清理语句处理程序

[英]Cleanup of statement handlers in DBI perl

I am using DBI perl to connect with Sybase dataserver. 我正在使用DBI perl连接Sybase数据服务器。 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. 目前,我正在每个函数后面执行undef $sth ,如someAnotherPerlFunctionOne中所示。 Is that necessary? 那有必要吗?

Perl将为您进行清理,但是最好将db句柄传递给函数,而不是每次都重新创建它并立即销毁它。

There are a few things to look out for, but using undef isn't one of them 需要注意一些事情,但是使用undef并不是其中之一

  • 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 您可以使用$dbh->ping

  • You should prepare each statement only once. 每个语句只prepare一次。 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 如果您在不获取所有数据的情况下留下了SELECT语句句柄,则应调用$sth->finish来重置它

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. 这是我认为应该如何工作的一个相当Perlish的伪代码示例。 Every time around the until ( $end_of_day ) loop the first thing is to check whether the database handle is still valid. 每次until ( $end_of_day )循环,第一件事就是检查数据库句柄是否仍然有效。 If not then there is the point when you should establish a connection and prepare all of your statement handles 如果没有,那么一点时,你应该建立一个连接,并prepare所有的语句句柄

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 您会注意到,为每个语句句柄完成prepare之后,两个子例程会折叠为同一代码

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM