I have created 5000 child process from a parent. Made a global database connection. Every child process will share that db connection. Here SIGALARM is handled. If the alarm is generated before the sleep(10000) by manually. The prepared DB query will be executed simultaneously.
While passing the signal to the child process. 5000 rows are updated successfully. but it has thrown following error message as
message type 0x5a arrived from server while idle
message type 0x43 arrived from server while idle
message type 0x5a arrived from server while idle
I am using DBI module to connect with database. I wanted to know whether DBI using thread?...
For your reference:-
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Data::Dumper;
if( $#ARGV < 0 ) {
die "no sufficient argument.\n";
}
my $fork_count = shift;
#print STDERR $local_time."\n";
#print STDERR $execute_time."\n";
my $user="zivah";
my $password="zivah";
my $database="zivah";
my $schema="public";
my $server="192.168.1.40";
my $port="5432";
my $logfile="Log.txt";
my $LOG_FILE;
open ($LOG_FILE, ">$logfile") or die "Can't open the file, $!!\n";
$SIG{"ALRM"} = \&handler;
sub handler() {
my $handle=$_[0];
my $time=localtime;
if($handle eq "ALRM") {
print $LOG_FILE "SIGNAL SIG$handle is generated on $ti
+me -- $$\n";
&Fork_Execute();
}
}
my $Database_handler;
my $sth;
sub Database_Connect {
# connect with database.
#print "Connecting Database\n";
unless( $Database_handler = DBI->connect("dbi:Pg:database=".$d
+atabase.";host=$server;port=$port", $user, $password, { RaiseError =
+> 0, AutoCommit => 1, PrintError => 1, InactiveDestroy => 1, pg_serve
+r_prepare => 0}) ) {
# Print an error message for unsuccessfull connection,
print "EMERG: connect_database : connecting database f
+ailed, $DBI::errstr" ;
# Return -1.
return -1 ;
}
# Return the database handler.
return $Database_handler;
}
sub Fork_Prepare {
my ($result, $query, $return_value, $delete_time, @tables);
# Prepare sql queries for finding unwanted tables.
#$query="INSERT INTO $schema.a(name) values('santhosh');";
my $id=shift;
$query="Update $schema.a SET name='single',mtime=now() where n
+o=$id;";
unless($sth = $Database_handler->prepare($query)){
# print an error message.
print "ERROR:can't prepare SQL statement:[ $query ]:[$
+DBI::errstr]";
# return -1.
return -1;
}
}
sub Fork_Execute {
my ($result);
unless($result = $sth->execute()){
# Print an error message
print "ERROR:can't execute SQL statement::[$DBI::errst
+r]";
return -1;
}
}
&Database_Connect();
my $fc=1;
my $cnt=1;
my @pids;
system("date");
while( $fork_count >= $fc ) {
my $pid = fork();
if (not defined $pid) {
print "resources not avilable.\n";
} elsif ($pid == 0) {
&Fork_Prepare($cnt);
#&Database_Connect();
sleep(1000);
exit;
} else {
$fc++;
$cnt++;
next;
}
}
system("date");