http://qs321.pair.com?node_id=295103

gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:

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