[Catalyst] Re: [Catalyst-dev] Bug in Catalyst::Model::DBI .15

Alex Pavlovic alex at taskforce-1.com
Mon May 7 07:16:37 GMT 2007


Hi,

On Sunday 06 May 2007 15:23, Evan Carroll wrote:
> On 5/6/07, Alex Pavlovic <alex at taskforce-1.com> wrote:
> > Please note that each model will still create single initial connection (
> > when request for $dbh arrives ). This happens with either approach (
> > squirreling away a connection in "new" or ref to undef scalar ).
>
> Thanks for the patch. That should work (haven't seen it yet) because
> all of my functions go through a proxy of sort anyway, M::DBI::Root.
>
> > However re-connect will not be made afterwards for each new request. This
> > is where the C::M::Proxy has advantage because single connection is
> > created and maintained for all models. I would strongly recommend that
> > you use it, rather then mucking around with ACCEPT_CONTEXT.
>
> Could you please clairfy what Model::Proxy has to do with
> ACCEPT_CONTEXT, and acccessing things like $c->user.

Well, one way of getting to the $dbh before was through the ACCEPT_CONTEXT 
because if you had single Root DBI model, you could just do:

my $dbh = $c->model('Root')->dbh;

from a model that did not inherit from that root model. The other alternative 
was to use C::M::Proxy instead. This way each model could get access to 
shared $dbh. There is no relation with the $c->user. 

> I'm just 
> confused: I don't understand that. Or whatever  advantages
> Model::Proxy has over just inheriting from a single model, which is
> what I currently do.

Each time you inherit from a single root model, a database connection has to 
be created for that specific child model. This is not a big overhead though, 
because it's done only once per model, unless the request is in a different 
process or thread ( which is a different story ). 

On the other hand C::M::Proxy will install into itself references to all proxy 
subroutines given to it during initiliazation. When caller's model requests a 
subroutine, proxy will transparently return it. This way you are always 
getting the single pre-opened database connection for all of your models. 

So to cut the story short, unless you re-implement similiar code used by the 
Proxy in your Root model, then you will still be opening connection initially 
each time you inherit, i.e. there will be separate connection channel for 
each model.

>
> If M::DBI::TableA, inherits from M::DBI::Root, which has a sub dbh {
> $c->model('DBI')->dbh ) then isn't this the same end result. As in all
> operations are executed through $c->model('DBI')->dbh?

First of all, $c->model('DBI')->dbh is invalid because C::M::DBI is catalyst 
model not your application's model. I am not exactly sure what your code is 
doing here. Maybe next time you can write more code to demonstrate what you 
mean. 

Perform a simple exercise, create two models ( A and B ), make these inherit 
from a third Root model which inherits from C::M::DBI. Make some $dbh calls 
from controller. Turn on debugging and watch the database connections.

>
> I look forward to having the new M::DBI hit cpan.

Should be there.

-- 
Alex Pavlovic - CTO
TF-1 Inc. ( Custom development, consultancy and training )
http://taskforce-1.com



More information about the Catalyst mailing list