Re: "Dynamic" database handles?

[prev] [thread] [next] [Date index for 2005/01/23]

From: Jason Gessner
Subject: Re: "Dynamic" database handles?
Date: 00:47 on 23 Jan 2005
you won't see the "variable $foo won't stay shared" i believe, but you  
will run into a problem very quickly if you use virtual hosts.

The problem with C::DBI is that your connection is a module variable  
and is never reinitialized (unless you force it to be).  What this  
means is that Virtual host A will start accessing Virtual host B's  
database if that particular apache child has already been used against  
virtual host A.  Sucks.

The way i had to fix this in a Mason application is to have my top  
level autohandler explicitly re-initialize the connection (which is  
cheap if you use Apache::DBI) based on an environment variable that was  
set in my httpd.conf.

-jason gessner
     jason@xxxxxxxx.xxx

On Jan 22, 2005, at 6:39 PM, Daniel Collis Puro wrote:

> Well,  I came up with:
>
> ##########
>
> package OOPC::DB;
>
> use base 'Class::DBI';
>
> our $dbh;
>
> sub db_Main{
>     return $dbh;
> }
>
> #CDBI package definitions here.
> 1;
>
> And then in my CGI::Application module:
>
> package OOPC::Settings::Manage;
> use strict;
> use warnings;
> use OOPC::DB;
>
> use base 'CGI::Application';
>
> sub setup{
>     my $self=shift;
>     my $dbh=$self->param('dbh');
>     $OOPC::DB::dbh=$dbh;
>     $self->start_mode('list_namespaces');
>
> $self->mode_param($self->param('settings')->{Global}- 
> >{run_mode_param});
>     $self->run_modes(
> 		     list_namespaces=>'list_namespaces',
> 		     is_denied=>'is_denied',
> 		     AUTOLOAD=>'list_namespaces'
> 		    );
> }
>
> 1;
>
>
> But I'm worried about the whole "variable will stay shared" error that
> can happen when you use "global" variables with mod_perl.
>
> Do folks think this will this be mod_perl safe?  Something about this
> just feels wrong.
>
> -DJCP
>
>
> On Sun, 2005-01-23 at 00:20 +0000, Matt S Trout wrote:
>> On Sat, Jan 22, 2005 at 06:44:30PM -0500, Daniel Collis Puro wrote:
>>> I'm looking to use Class::DBI in an application where the DB  
>>> connection
>>> info is passed in at run time. I'm building a generic e-commerce  
>>> system
>>> around CGI::Application, HTML::Template and a few of my other  
>>> favorite
>>> modules.
>>>
>>> The idea is I'd set up the relationships between tables in separate
>>> classes via Class::DBI and then use the CDBI classes in my
>>> CGI::Application modules along with Class::DBI::AsForm.
>>>
>>> I must be dense, because I can't figure out a clean way to pass a DBI
>>> handle or DSN info at run time, and I want this system to be generic
>>> enough to completely avoid hard-coding database connections.
>>>
>>> What am I missing? This is all meant to run under mod_perl, so please
>>> keep this in mind.
>>
>> I'm doing the same sort of thing at the moment; contact me off-list  
>> if you're
>> interested in a technology share.
>>
>> Meantime, try overriding the db_Main method like so -
>>
>> package CDBI::BaseClass;
>>
>> #__PACKAGE__->connection(...) Don't need this now
>>
>> sub db_Main {
>>   return DBI->connect_cached(...); # Insert runtime DSN selection here
>> }
>>
> -- 
> Dan Collis Puro
> Chief Engineer
> GeekUprising Internet Consultants
> http://www.geekuprising.com
> dan@xxxxxxxxxxxx.xxx
> 781-775-1338
>

(message missing)

"Dynamic" database handles?
Daniel Collis Puro 23:44 on 22 Jan 2005

Re: "Dynamic" database handles?
Matt S Trout 00:20 on 23 Jan 2005

Re: "Dynamic" database handles?
Daniel Collis Puro 00:39 on 23 Jan 2005

Re: "Dynamic" database handles?
Jason Gessner 00:47 on 23 Jan 2005

Re: "Dynamic" database handles?
Perrin Harkins 01:04 on 23 Jan 2005

Re: "Dynamic" database handles?
William McKee 16:03 on 24 Jan 2005

Re: "Dynamic" database handles?
Dana Hudes 16:17 on 24 Jan 2005

Re: "Dynamic" database handles?
jason 18:59 on 24 Jan 2005

Re: "Dynamic" database handles?
Michael Graham 19:01 on 24 Jan 2005

Re: "Dynamic" database handles?
Perrin Harkins 00:50 on 23 Jan 2005

Re: "Dynamic" database handles?
jason 19:24 on 24 Jan 2005

Re: "Dynamic" database handles?
Michael Graham 21:39 on 24 Jan 2005

Generated at 17:42 on 27 Jan 2005 by mariachi v0.52