DBIx :: Class gets dbh

I am using DBIx :: Class in Catalyst. I am creating an application. It works fine, but sometimes I need to use my own db functions that I developed that are very specific to my needs. Because of this, I need dbh. However, since I already use DBIx::Class , I know that it already uses the dbh that it uses. To avoid another unnecessary database connection, I would just like to use dbh, which DBIx::Class has already been created. I know that the DBIx :: Class :: Storage :: DBI module has two methods, dbh and dbh_do , but I'm not quite sure what the difference is between the two, and if this is the best way to access dbh. Can someone tell me that the best way to get dbh from DBIx::Class would be in a Catalyst application? I would prefer a method that I could forward to save dbh in stash, as shown below:

 sub dbh :Private { my ($self, $c) = @_; $c->stash->{dbh} = #get dbh from DBIx::Class here } 

Thanks!

+4
source share
2 answers

I always need to see this. Assuming you have an instance of a schema object, you can get its Storage object using the storage method. Assuming Storage::DBI , there is an accessible dbh method that will provide you with a database handle. So:

 my $dbh = $c->model( 'My::DB' )->storage->dbh; 

gotta do the trick.

+9
source

@srchulo The answers are great, and dbh_do is a way to use the built-in exception handling, but I suggest converting your function so that you no longer use dbh and just work with DBIX :: Class. So the next time you just need to change in one place and not continue to look for obsolete dbh and raw sqls. Hope this makes sense.

-1
source

All Articles