在表上插入重复值期间发生超时问题

时间:2016-12-21 04:55:28

标签: perl cgi dbi

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

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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