2
votes

I'm using DBIx::Class in a Catalyst app, and I was wondering if it is possible to make HTML::FormHandler use the same dbh that was already used by DBIx::Class earlier in the code (perhaps it already does this?). For example:

#connect to db and make query
my $val = $c->model('DB::Example')->search({ condition => 'y'});

my $form = myapp::Form::Example->new;

#now reuse dbh here when updating somehow??
return unless $form->process(
    schema  => $c->model('DB')->schema,
    item_id => $c->user->id,
    params => $c->req->params,
); 

I know that even if it is creating two database handles it's not gonna kill performance, but I would like to be as concise as I can whenever possible :)

Also, if I were to make to DB calls using DBIx::Class one after the other, does that reuse the same dbh? Thanks!

2

2 Answers

3
votes

Each DBIx::Class::Schema instance has its own dbh. If you fork it will take care to create a new database connection per process as well.

So if you pass the Catalyst model instance to Formhandler it will reuse the dbh.

0
votes

Passing dbh may not be what you want, The whole idea of MVC is provide abstraction and all the solutions suggested violates it. You can keep the dbh (using dbh may not be best idea but still) in controller and fetch the data and stash it in html using $c->stash('what/whatever.html'). So that way, front end does have to deal with any data fetching.