[Dbix-class] Getting a database handle

Brandon Black blblack at gmail.com
Wed Jan 3 22:02:18 GMT 2007


> Why is long-term storage bad?

Because the handle it returns to you is totally unmanaged.  We can't
do anything for you if your database server rebooted or timed out your
connection while you were hanging onto a copy of the $dbh returned to
you (or for that matter if you spawned a fork or thread, etc).  On the
other hand, if you always fetch a fresh one from $schema->storage->dbh
just before you need it, the Storage layer will ensure you always get
a good handle to work with.

> Is refreshing it every request short
> enough? The DBIx::Class::Storage::DBI docs do not give any details on
> why long-term storage is bad.
>

Every request is fine.  Its probably cleaner to just grab it in each
sub that uses, as in:

sub some_action : Local {
 ...
 my $dbh = $c->model('Foo')->schema->storage->dbh;
 $dbh->selectall_hashref(...);
  ...
}

-- Brandon



More information about the Dbix-class mailing list