How can I share a database connection across a for

2019-08-27 01:42发布

问题:

I made the following programs in Perl before:

my $db = DBconnection with DB2

if ($pid = fork()) {
    #parent
} else {
    #child
    $db->execute("SELECT ****");
    exit;
}

wait();
$db->execute("SELECT ****");

I thought that it waited for the end of the child process to have wanted to do it and would operate it for DB by a pro-process.

In addition, DB is not connected to the contents of the error.

What's wrong?

回答1:

There is a lot of stuff you must do to allow a child process to use its parent's DBI handle. See this article on Perl Monks about DBI, fork, and clone.



回答2:

Try including this line of code in your child block:

$db->{InactiveDestroy} = 1;


标签: perl db2 fork