Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

help with afork and dbi

by bplegend (Novice)
on Jan 17, 2011 at 23:19 UTC ( #882772=perlquestion: print w/replies, xml ) Need Help??

bplegend has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I tried to make use of the afork procedure with a DBI call but I don't know why it kept gives me this lost contact error at the end. What would you suggest? I kindly appreciate your input. Thank you ben
--- test.pl --- use DBI; use DBD::Oracle qw(:ora_session_modes); my $tbs_name='T2010'; my $dbh = DBI->connect("DBI:Oracle:","","",{ora_session_mode => ORA_SY +SDBA,RaiseError=>1,AutoCommit=>1,PrintError=>1}); my $sql = sprintf qq{SELECT DISTINCT TABLE_OWNER,TABLE_NAME FROM SYS.D +BA_TAB_PARTITIONS WHERE TABLESPACE_NAME=? AND TABLE_OWNER='FOO'}; my $sql_h = $dbh->prepare($sql); $sql_h->execute($tbs_name); $sql_ref = $sql_h->fetchall_arrayref; afork ($sql_ref,10,\&initialize_control_data,$dbh); $dbh->disconnect(); exit(0); sub afork (\@$&&) { my ($data,$max,$code,$cdbh) = @_; my $c = 0; foreach my $data (@$data) { wait unless ++$c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); $cdbh->{InactiveDestroy} = 1 if ($pid); exit $code->($cdbh,@$data[0],@$data[1]) unless $pid; } 1 until -1 == wait; } sub initialize_control_data { my ($edbh,$schema,$table) = @_; print "$schema,$table\n"; } --- end --- % test.pl FOO,TABLE1 ... FOO,TABLEn DBD::Oracle::db disconnect failed: ORA-03135: connection lost contact +(DBD ERROR: OCISessionEnd) at ./test.pl line 20. DBD::Oracle::db disconnect failed: ORA-03135: connection lost contact +(DBD ERROR: OCISessionEnd) at ./test.pl line 20.

Replies are listed 'Best First'.
Re: help with afork and dbi
by tilly (Archbishop) on Jan 17, 2011 at 23:25 UTC
    My first piece of advice is to lose the prototypes. Really. They don't do what you think they do, and they cause a lot of non-obvious problems.

    With that said, your problem is described in http://search.cpan.org/~timb/DBI-1.616/DBI.pm#InactiveDestroy. When you fork, each copy of your process winds up talking over the same database connection. So the database gets both talking to it at once, and when the parent says, "Disconnect now", it does. Which means that the child is now talking to a database that is no longer listening.

    The usual fix is to have the connection happen after the fork. Alternately you can set InactiveDestroy to false, and be very, very careful to only ever use the database connection from one copy of the process. (Because if 2 copies are reading and writing to the same connection at the same time, the database is going to become very confused.)

Re: help with afork and dbi
by kyle (Abbot) on Jan 18, 2011 at 16:15 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://882772]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2021-05-18 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (185 votes). Check out past polls.

    Notices?