http://qs1969.pair.com?node_id=219459


in reply to Re: forking and dbi
in thread forking and dbi

I have a feeling that all of the child processes should be able to share the same $parent_dbh.

The problem is that when you fork, you duplicate the entire contents of the current process. However, sometimes a process has certain characteristics that are not pure data, but that are tied specifically to the process in which they were initiated. In the case of database handles, the issue is the socket connections established prior to the fork that uniquely relate the process that created them to the database itself. Those sockets can't be treated merely as pure data and hence cloned, because they constitute a relationship between two processes. And that relationship gets broken during the cloning procedure.

AFAIK the only way to fork and share a database handle is to use interprocess communication, or to have some kind of "smart" forking procedure that reexecutes particular pieces of the code that occur before the fork and which cannot be directly cloned. But that would be hard :^P

Replies are listed 'Best First'.
Re: Re: Re: forking and dbi
by mpeppler (Vicar) on Dec 13, 2002 at 16:52 UTC
    There are several issues with forking and using database handles opened in the parent.

    At its most basic the underlying database client libraries must support this - the Sybase libraries do, but I have heard that the Oracle libraries do not.

    Second, because the client usually maintains state associated with the connection you need to make sure that no operation is pending at the time of the fork. Otherwise you are likely to have a very confused connection.

    Third, DBI maintains some state as well, and this has to be taken into consideration as well.

    I know for a fact that I can fork a perl script that uses Sybase::CTlib and use the connection in the child. I can even fork it multiple times and use the same connection in multiple children, as long as the use is coordinated so that only one child has access to the connection at a time (see for example my Apache::Sybase::ConPool module.)
    I have not tried this with DBI, but I would expect it to work, as long as no open statements exist at the time of the fork.

    Michael