perlquestion
gnu@perl
<P>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.
<P>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?
<P>Here is what I get when I run the code below:
<code>
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)
</code>
<i>snip...</i><code>
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;
}
}
</code><i>...snip</i>