Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

DBIx::Class connection

by morgon (Curate)
on Jan 02, 2011 at 21:25 UTC ( #880093=perlquestion: print w/replies, xml ) Need Help??
morgon has asked for the wisdom of the Perl Monks concerning the following question:


a quick DBIx::Class question:

MySchemaClass->connect(...) does not immediately connect to the database, but this seems to be done in a "lazy" way, not before a connection is really needed.

I would now like to create a log-message when the physical connection to the database is established, so I wonder how to register a callback with DBIx::Class to get notified when it connects.

Is that possible?

Many thanks!

Replies are listed 'Best First'.
Re: DBIx::Class connection
by Your Mother (Bishop) on Jan 02, 2011 at 23:46 UTC

    You could pass a sub instead of plain connection variable and do whatever you want there.

    $schema= My::Schema->connect(sub{ # Prep/config load. my $dbh = DBI->connect... # Do your logging. return $dbh; }

    I'm not sure it's the easiest/sanest way. It feels like the wrong place to do it to me. Perhaps you want some permutation of DBI->trace instead.

      yes, as far as I remember it's not recommended to do the DBI connect yourself since DBIC cares about reconnecting itself when the connection has gone away, so tracing in DBI is probably the best way.
      As always many thanks for sharing your knowledge, mother.

      And to log the SQL that is generated I could simply subclass DBIx::Class::Storage::Statistics and set it as debugobj I guess?

      And while I am at it (and don't want to experiment when I can simply ask mother):

      I don't quite understand what happens when the database-connection is lost (I am using mysql).

      I understand DBIx::Class will try to reconnect automatically - does that mean it will try once and give up if it fails to reconnect or what exactly happens?

      How could I trace (assumed I cared) when a connection is lost and re-established?

      Many thanks.

        Look at DBIx::Class::QueryLog and Log::Log4perl::Appender::DBIx::Class and the trace section of DBI. I think you can do pretty deep logging but I've never tried and when I've needed a log I've generally used DBI->trace to a tmp file.

        I also am not sure how the reconnection stuff is is handled. I've been lucky enough that it's always "just worked" for everything I've done. You could dive the guts of the relevant Storage/Engine modules though I suspect the reconnect stuff is (at least partly) handled in the relevant DBD modules.

      It feels like the wrong place to do it to me.

      This is very much the case yes. See my answer to the OP.


Re: DBIx::Class connection
by Khen1950fx (Canon) on Jan 03, 2011 at 09:16 UTC
    I took Your Mother's example and added DBIx::QueryByName::Logger and DBIx::Connector. DBIx::Connector will automatically take care of reconnecting your $dbh. All you have to do is worry about exceptions. If you have Log::Log4perl, then DBIx::QueryByName::Logger will use its get_logger method and utilize Log::Log4perl.
    package My::Schema; use strict; use warnings; use base qw/DBIx::Class::Schema/; use DBIx::Connector; use DBIx::QueryByName::Logger; __PACKAGE__->load_classes; my $dsn; my $username; my $password; my %attr; my %extr; my $schema = My::Schema->connect( sub { my $conn = DBI::Connector->new( $dsn, $username, $password, \%attr, \%extr); my $dbh = $conn->dbh; $conn->run( fixup => sub { my $log; &$_->($log = get_logger()); }); });
      DBI::Connector->new or DBIx::Connector->new
Re: DBIx::Class connection
by ribasushi (Monk) on Mar 25, 2013 at 12:03 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://880093]
Approved by ww
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-06-24 21:22 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.