Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

DBI + fork ... child process kills handle

by DrManhattan (Chaplain)
on Jan 25, 2006 at 22:43 UTC ( [id://525589] : perlquestion . print w/replies, xml ) Need Help??

DrManhattan has asked for the wisdom of the Perl Monks concerning the following question:

I'm having some difficulty with a program that establishes a database handle and then forks. When the child process exits, the database handle disconnects. Here's a conceptual example:
#!/usr/bin/perl use strict; use MyDBH; my $dbh = MyDBH->new(); print "Ping: ", $dbh->ping(), "\n"; my $kidpid = fork(); if ($kidpid == 0) { # child process print "Hello world from $$!\n"; exit; } # parent process print "Spawned child process: $kidpid\n"; print "Ping: ", $dbh->ping(), "\n";
Output looks like this:
Ping: 1
Hello world from 7022!
Spawned child process: 7022

I think what's happening here is that the database handle's destructor is being called when the child process exits. If I replace the exit() call in the child with an exec() of something, the child process is replaced in memory and the destructor is never called. That seems kind of kludgey to me though, and I'm wondering if there's some other way around this. Is there any other way I can remove the object from the child's space or prevent the destructor from being called? I realize I can disconnect the handle prior to forking and the reestablish it, but I'm hoping there's a cleaner solution.


Replies are listed 'Best First'.
Re: DBI + fork ... child process kills handle
by rnahi (Curate) on Jan 25, 2006 at 22:51 UTC

    You may want to have a look at the InactiveDestroy attribute.

    An example of its usage is in an old node (DBI 1.39).