Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Sharing a database connection across fork()

by wrog (Friar)
on Sep 11, 2014 at 14:31 UTC ( [id://1100308]=note: print w/replies, xml ) Need Help??


in reply to Sharing a database connection across fork()

Things to keep in mind:
  1. DESTROY on a connection will, for many db drivers, do Something Bad to the connection that renders it useless in all processes that it's been copied to. Setting $dbh->{InactiveDestroy}=1 in a particular process prevents that Something Bad from happening in that process. However the Something Bad is most likely something that needs to happen once.

    Therefore, you want to set $dbh->{InactiveDestroy} in every process except whichever one is going to be the last one to be holding onto the connection (usually, that's the parent but not always; depends on how your program actually works)

    Or you want to set $dbh->{AutoInactiveDestroy} as soon as you create it, provided that the process that's creating is also going to be last/only one to be using it, which is often the case (but again not always). And this option is also best if there can be forks happening behind your back.

  2. And in the vast majority of case where I said "last/only", you really want that to be "only", i.e., you almost never want to be actually using (reading/writing) the connection from more than one process. Not if the database driver has any kind of state that needs to be kept on the client side of the connection. Because if that's so, then once you do something in one process, that will likely change the state and from then on attempts to use the connection from another process will fail.

  3. And even if that's not the case and you actually can do stuff safely from more than one process (say, because, you're on Windows, and fork isn't actually fork, and the DB driver author took the trouble to write a CLONE routine that does the Right Thing with all of the client-side state), you still need to be doing synchronization so that no two process are attempting to read or write the same connection at the same time.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found