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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|