dbi_dbh_shared_by_forked_children

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: Eric Kolve <ekolve@home.com>
From: "Sterin, Ilya" <Isterin@ciber.com>
Subject: RE: sharing dbh across forked children
Date: Sun, 11 Mar 2001 23:46:35 -0500


drdoctor [mailto:drdoctor]On Behalf Of Eric Kolve wrote:

> Is it possible to share a database handle initialized in a parent with
> forked children ?  What I want to do is process a file which needs to
> execute many SELECT statements in parallel.  I am wondering if each
> child needs to instantiate its own dbh or whether you can execute sql
> simultaneously against the same dbh.

You can have many statement handles for each dbh.  I think this is what you
are refering to.

$dbh = DBI->connect(.....);
$sth = $dbh->prepare(.....);
$sth2 = $dbh->prepare(......);
$sth->execute();
$sth2->execute();

Each query is bound to it's statement handle where all of the statment
handles can then in fact be bound to one dbh.

===

To: dbi-users@perl.org
From: "Matthew O. Persico" <persicom@acedsl.com>
Subject: Re: sharing dbh across forked children
Date: Sun, 11 Mar 2001 23:59:22 -0500

"Sterin, Ilya" wrote:
> 
> You can have many statement handles for each dbh.  I think this is what you
> are refering to.
> 
> $dbh = DBI->connect(.....);
> $sth = $dbh->prepare(.....);
> $sth2 = $dbh->prepare(......);
> $sth->execute();
> $sth2->execute();
> 
> Each query is bound to it's statement handle where all of the statment
> handles can then in fact be bound to one dbh.

Yes, but I think that Eric wants to know what happens if the sth's are
created in forked children. As I understand it, the sth creation has to
do bookkeeping in the dbh. Now, if two different modifications are made
to two copies of the dbh, yet the underlying connection to the database
api is not duped at the fork, I think all hell can break loose. The
safest thing to do is fork, then open dbh, then open sth, then be gone.


===

To: Eric Kolve <ekolve@home.com>
From: Jonathan Leffler <jleffler@earthlink.net>
Subject: Re: sharing dbh across forked children
Date: Sun, 11 Mar 2001 23:33:17 -0800

Eric Kolve wrote:

> Is it possible to share a database handle initialized in a parent with
> forked children ?  What I want to do is process a file which needs to
> execute many SELECT statements in parallel.  I am wondering if each
> child needs to instantiate its own dbh or whether you can execute sql
> simultaneously against the same dbh.

As Matthew Persico said, it is not safe to create a $dbh, fork children,
and then have any of the children try to access the database via the
pre-created $dbh.

What Matthew didn't mention is that the documentation says that this is
the case -- see the Cheetah book.  ...time passes...  I went to get a page
reference, and I'm not sure that I came up with a good one: p204
(InactiveDestroy) discusses it a bit indirectly.

Tim -- suggestion for a future edition of book/DBI specification: it
should be explicitly called out that using a $dbh across even a single
fork() may not work at all, even if the parent does not attempt to use the
handle, let alone attempting to use it across multiple fork.

And, as a specific f'r'instance, DBD::Informix will not work reliably if
you try that -- the connection is associated with a specific PID and the
children have the wrong PID (process ID).

===

To: Eric Kolve <ekolve@home.com>
From: Michael Peppler <mpeppler@peppler.org>
Subject: Re: sharing dbh across forked children
Date: Mon, 12 Mar 2001 06:05:51 -0800 (PST)

Eric Kolve writes:
 > Is it possible to share a database handle initialized in a parent with
 > forked children ?  What I want to do is process a file which needs to
 > execute many SELECT statements in parallel.  I am wondering if each
 > child needs to instantiate its own dbh or whether you can execute sql
 > simultaneously against the same dbh.

Hi Eric!

The answer is: it depends.

With Sybase it works (that's how I implement connection pooling in the
Apache process). My understanding from Tim B. is that this doesn't
work with Oracle, however.

===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu