in reply to Sharing a database connection across fork()
Things to keep in mind:
-
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.
-
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.
-
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.
|