I have a perl program in which I need to fork a child process to do something else. In both the parent process and the child process, there are database interactions. I have a perl module in which the database connection is initiallized. But I don't have disconnection in the same module. I rely on the calling programm to automatically disconnect when the calling program terminates. It works perfect without child process. But with a child process created in the middle of the perl program and exits before the parent process. I have trouble with the database connections. As when the child process terminates, it seems disconnect the database handler therefore the parent process isn't able to continue its job with the database. Here is the program:
--------------------------- mail program -------------- myquery ('3933'); #this is parent process $| = 1; # turn off buffering $newpid = fork(); if ( not defined $newpid ){ # if return value of fork() is undef, something went wrong die "fork didn't work: $!\n"; } elsif ( $newpid == 0 ){ # This is what the child process does print "inside child---------\n"; myquery ('3934'); exit( 0 ); } myquery ('3932'); ----------------one subroutine--------------------- sub myquery { my ($target_member_id) = @_; my ($query) = "select username from member where id=$target_member +_id"; my ($sth) = $mypackage::dbh->prepare ($query); $sth-> execute; my ($row) = $sth->fetchrow_array; if($row) { print "row: $row\n"; } else { print "empty\n"; } $sth->finish; } ---------------the perl module to establish the db connection ------ package mypackage; use DBI; $dbh = DBI->connect("$DB_SERVER:$DB_NAME;$DB_HOST",$DB_USERNAME,$DB_PA +SSWORD) || die "Content-type: text/html\n\nCan't connect to $dbname: $DBI::errstr\n"; ... ...
Thanks.

In reply to Access the same database in two processes by sachaer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.