【问题标题】:Timeout issue during insert duplicate value on table在表上插入重复值期间的超时问题
【发布时间】:2017-05-06 10:31:12
【问题描述】:

在运行脚本之前,我插入了表值。

INSERT INTO table4 VALUES('art','best') 表在第一个字段上有唯一约束。有我的 cgi 代码示例:

cat action.pl
#!/usr/bin/perl
use strict;
use warnings;
use subs;
use CGI;
use DBI;
use DBD::Oracle;
$|++;
my $dbh;


my $db_name="Oracle:xe";
my $user="SYSTEM";
my $password="ps";

my %first_table_object=(
    column_names => [ "filed1" , "field2" ],
    table_name =>  [ "table4" ]
);


my %second_table_object=(
    column_names => [ "BETA" , "PAST" ],
    table_name =>  [ "table3" ]
);
print qq(Content-type: text/html\n\n);
my $html = <<EOT;
EOT
;
print $html;
eval{
    $dbh = DBI->connect("DBI:$db_name",$user, $password,
                       {
                            'RaiseError' => 1,
                            ShowErrorStatement => 1,
                            PrintError=>1 
                        });
};
if( $@ ){
    print $@;
    die("couldn't connect $@");
}

my $query = new CGI;
my $value1= $query->param('textbox');
my $value2= $query->param('dropDownvalue');
my $index=$query->param('index');
my $sth;
eval{
    my %current_hash_obj;
    if ( $index == 0 ){
        # We may change to INSERT INTO table_name(column1,column2)  VALUES ( ?, ? )
        $sth = $dbh->prepare("INSERT INTO $first_table_object{table_name}[0] ($first_table_object{column_names}[0] , $first_table_object{column_names}[1] )  VALUES ( ?, ? )") or die $dbh->errstr;
    }
    else{
        # we may change to INSERT INTO table_name(column1,column2)  VALUES ( ?, ? )
        $sth = $dbh->prepare("INSERT INTO $second_table_object{table_name}[0] ($second_table_object{column_names}[0] , $second_table_object{column_names}[1] )  VALUES ( ?, ? )") or die $dbh->errstr ;
    }
    if(defined($sth)){
        $sth->execute($value1,$value2);
    }
    else{
        $dbh->disconnect;
        die("Sth not defined");
     }
    #$dbh->do("INSERT INTO $inserted_table VALUES (  $value1, $value2 )");
};

if( $@ ){
    print $@;
    $dbh->disconnect;
    die("ERROR $@");
}
print "Succesfuly inserted";

我通过调用来运行 CGI 脚本

http://127.0.0.1/cgi-bin/action.pl?index=0&dropDownvalue=TYPE_MISC$&textbox=art&_=1482294800012

我在日志中得到了这个

[Tue Dec 20 23:48:48 2016] [warn] [client 127.0.0.1] Timeout waiting for output from CGI script /var/www/cgi-bin/action.pl

[Tue Dec 20 23:48:48 2016] [error] [client 127.0.0.1] (70007)The timeout specified has expired: ap_content_length_filter: apr_bucket_read() failed

如何解决超时问题?我想查看唯一约束错误而不是超时错误

【问题讨论】:

    标签: perl cgi dbi


    【解决方案1】:

    您的代码需要模块“CGI::Carp - 用于写入 HTTPD(或其他)错误日志的 CGI 例程”。这样 Perl 错误就会记录在 HTTPD 日志中。请在http://perldoc.perl.org/CGI/Carp.html学习一下,了解如何使用它。

    然后,您可以在执行插入记录语句之后立即插入以下代码。我认为 $sth->errstr 将包含唯一约束错误。您可能会故意插入重复记录并打印它(print $sth-&gt;errstr;)来测试它。

    if( $sth->err ) { # an error has occurred
       die "Insert error: ". $sth->errstr;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2012-07-30
      • 2012-02-27
      相关资源
      最近更新 更多