Re: [CDBI] prepared statements and InactiveDestroy

[prev] [thread] [next] [Date index for 2005/11/07]

From: Perrin Harkins
Subject: Re: [CDBI] prepared statements and InactiveDestroy
Date: 18:08 on 07 Nov 2005
On Mon, 2005-11-07 at 11:59 +0100, Aaron Ross wrote:
> Hi,
> 
> > I'm writing a Class::DBI app which fork()'s where both parent and  child 
> > need to keep the DBI handle.  Hence, I'm using  $dbh-> {InactiveDestroy} 
> 
> You can't keep a DB handle open across a fork. Reconnect in the child 
> using the same parameters.

Yes, but also set InactiveDestroy in the child, or the original handle
will no longer work in the parent.  (This took a lot of debugging to
figure out.)  It may differ by database, but this is definitely the case
with MySQL.

Here's a modification we made to Ima::DBI which makes it auto-reconnect
if you try to use it in a forked process:

sub _mk_db_closure {
	my ($class, $dsn, $user, $pass, $attr) = @_;
        $attr ||= {};
    
	my $dbh;
	my $process_id = $$;
	return sub {
		# set the PID in a private cache key to prevent us
		# from sharing one with the parent after fork.  This
		# is better than disconnecting the existing $dbh since
		# the parent may still need the connection open.  Note
		# that forking code also needs to set InactiveDestroy
		# on all open handles in the child or the connection
		# will be broken during DESTROY.
		$attr->{private_cache_key_pid} = $$;

                # reopen if this is a new process or if the connection
                # is bad
		if ($process_id != $$ or 
                    not ($dbh && $dbh->FETCH('Active') && $dbh->ping)) {
                    $dbh = DBI->connect_cached($dsn, $user, $pass, $attr);
                    $process_id = $$;
		}
		return $dbh;
	};

}

- Perrin


_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

[CDBI] prepared statements and InactiveDestroy
Aaron Turner 01:21 on 05 Nov 2005

Re: [CDBI] prepared statements and InactiveDestroy
Perrin Harkins 18:08 on 07 Nov 2005

Generated at 20:55 on 09 Nov 2005 by mariachi v0.52