2

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 Answers 2

3

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.

2
  • Do you know the syntax for passing the catalyst model instance to formhandler?
    – srchulo
    Commented Feb 5, 2013 at 7:06
  • 2
    You already do it in your code with schema => $c->model('DB')->schema. Commented Feb 5, 2013 at 16:54
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.