Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Using fork with DBI to create simultaneous db connections.

by VSarkiss (Monsignor)
on Feb 08, 2005 at 02:03 UTC ( [id://428887]=note: print w/replies, xml ) Need Help??


in reply to Using fork with DBI to create simultaneous db connections.

In general, you can't use a DBH across a fork. I don't know of any db clients where the handle will survive a fork.

You write, however,

it appears that all the children end up sharing the same DBH
How did you check for that? The good way to check would be to have your program fork, let each child open a connection, then sleep for a minute or something. During that time, log on to the database using another tool (Toad or DB-Artisan or something) and see how many connections are in your name. If the numbers agree, you're done.

Replies are listed 'Best First'.
Re^2: Using fork with DBI to create simultaneous db connections.
by thor (Priest) on Feb 08, 2005 at 04:26 UTC
    The good way to check would be to have your program fork, let each child open a connection, then sleep for a minute or something. During that time, log on to the database using another tool (Toad or DB-Artisan or something) and see how many connections are in your name. If the numbers agree, you're done.
    ...or, you could have each child just print the database handle. Like all perl objects, it's just a reference, so it should print something like DBI::db=HASH(0x1223456). If they all print the same reference, done. No monkeying around on the database at all (well, except the DBI calls that is...but you already knew that :P).

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      Actually, that's incorrect, and it's probably the same mistake that the original poster made. The flaw is that the memory address shown as part of the stringification of the reference is the *relative* memory address to that process. The operating system uses something called "virtual memory", which abstracts the actual *physical* memory location away from your processes. In truth, the physical memory addresses may change from time to time, as your process is swapped and paged and so on.

      Anyway, here's a quick demo:

      prompt> perl -le 'my $pid = fork; my $x = [$pid]; print "$x->[0] -- $x +"' 0 -- ARRAY(0x80d11ec) 24694 -- ARRAY(0x80d11ec) prompt>
      You can see that each process has it's own array, with the reference stored in $x, yet in both processes, the memory address appears the same.
      ------------ :Wq Not an editor command: Wq
        Hell...I learned something. Not that that's bad...I just don't like being wrong. :)

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://428887]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found