From all the doc I can find DBI::clone() should create a duplicate of the established connection by using the same information originally used to connect. I have a process that forks of child processes and I want each one to have their own connection to the DB.

The problem is that the clone() gives all the child processes the same handle. I was under the assumption that the code for the child process would not be executed until after the fork, if this is so then each child should get a unique connection. Can someone explain why each child proc has the same handle?

Here is what I get when I run the code below:

Parent db handle is DBI::db=HASH(0x7381e8) I'm child 6302 with db handle DBI::db=HASH(0x74fc2c) I'm child 6303 with db handle DBI::db=HASH(0x74fc2c) I'm child 6304 with db handle DBI::db=HASH(0x74fc2c) I'm child 6305 with db handle DBI::db=HASH(0x74fc2c) I'm child 6306 with db handle DBI::db=HASH(0x74fc2c) I'm child 6308 with db handle DBI::db=HASH(0x74fc2c) I'm child 6307 with db handle DBI::db=HASH(0x74fc2c) I'm child 6310 with db handle DBI::db=HASH(0x74fc2c) I'm child 6309 with db handle DBI::db=HASH(0x74fc2c)
snip...
for (my $counter = 1;$counter < $MAXCHILDREN;$counter++) { if (my $pid = fork) { # in parent, do nothing } elsif (! defined $pid) { print "fork failed! -->$!\n"; exit; } else { my $new_dbh = $dbh->clone(); print "I'm child $$\n\twith db handle $new_dbh\n"; exit; } }
...snip

In reply to Using DBI::clone() by gnu@perl

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.