Timeout issue during insert duplicate value on table

72 views Asked by At

Before running script I inserted into the table values.

INSERT INTO table4 VALUES('art','best') the table has unique constraint on first field . There are sample of my cgi code:

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

I run CGI script by calling

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

and I got this in the log

[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

How can I resolve timeout issue? I would like to see unique constraint error instead of timeout error?

1

There are 1 answers

0
csay On

Your code need the module "CGI::Carp - CGI routines for writing to the HTTPD (or other) error log". So that the Perl error will be logged in HTTPD log. Please study a bit in http://perldoc.perl.org/CGI/Carp.html to learn how to use it.

Then, you might insert below code right after the execution of the insert record statement. I supposed the $sth->errstr will contain the unique constraint error. You might test it intentionally inserting a duplicate record and print it (print $sth->errstr;).

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