File Coverage

blib/lib/Rose/DBx/AutoReconnect.pm
Criterion Covered Total %
statement 18 33 54.5
branch 0 4 0.0
condition 0 2 0.0
subroutine 6 12 50.0
pod 6 6 100.0
total 30 57 52.6


line stmt bran cond sub pod time code
1             package Rose::DBx::AutoReconnect;
2 1     1   25207 use strict;
  1         3  
  1         36  
3 1     1   6 use warnings;
  1         2  
  1         28  
4 1     1   6 use Carp;
  1         12  
  1         74  
5 1     1   6 use base qw( Rose::DB );
  1         2  
  1         1607  
6 1     1   1180541 use Rose::DBx::Cache::Anywhere;
  1         4  
  1         122  
7              
8             our $VERSION = '0.04';
9              
10             __PACKAGE__->db_cache_class('Rose::DBx::Cache::Anywhere');
11             __PACKAGE__->use_private_registry(1);
12              
13             DBI->trace(1) if $ENV{PERL_DEBUG} && $ENV{PERL_DEBUG} > 1;
14              
15             use Rose::Object::MakeMethods::Generic (
16 1     1   7 'scalar --get_set_init' => [qw( debug logfh )], );
  1         1  
  1         13  
17              
18              
19             =head1 NAME
20              
21             Rose::DBx::AutoReconnect - Rose::DB with auto-reconnect to server
22              
23             =head1 SYNOPSIS
24              
25             use Rose::DBx::AutoReconnect;
26             my $db = Rose::DBx::AutoReconnect->new;
27             $db->logger('hello world');
28              
29             =head1 DESCRIPTION
30              
31             Rose::DBx::AutoReconnect is a subclass of Rose::DB. Additional features
32             include:
33              
34             =over
35              
36             =item
37              
38             If using new_or_cached() method, will ping() dbh on every fetch from
39             cache to ensure that the dbh is connected. This extends
40             the basic Rose::DB::Cache behaviour beyond mod_perl/Apache::DBI.
41              
42             =item
43              
44             Convenient logger() method for debugging.
45              
46             =item
47              
48             Always uses DBI connect_cached() method when creating handles.
49              
50             =back
51              
52             Rose::DBx::AutoReconnect was written to allow new_or_cached() to
53             work with MySQL's "morning bug" and to allow for restarting
54             your database without restarting your application.
55             See also Rose::DBx::Cache::Anywhere.
56              
57             =head1 METHODS
58              
59             =cut
60              
61             =head2 init_logfh
62              
63             Defaults to STDERR. Get/set it with logfh().
64              
65             =cut
66              
67 0     0 1   sub init_logfh { *STDERR{IO} }
68              
69             =head2 init_debug
70              
71             Defaults to value of PERL_DEBUG env var (if set) or 0 (false).
72             Get/set it with debug().
73              
74             =cut
75              
76 0 0   0 1   sub init_debug { $ENV{PERL_DEBUG} || 0 }
77              
78             =head2 logger( I )
79              
80             Writes I to the filehandle set with logfh().
81              
82             =cut
83              
84             sub logger {
85 0     0 1   my $self = shift;
86 0           my @msg = @_;
87 0           for my $m (@msg) {
88 0           print { $self->logfh } join( ' ', $self->loglabel, $m, "\n" );
  0            
89             }
90             }
91              
92             =head2 loglabel
93              
94             Returns a pretty timestamp and label. Used by logger().
95              
96             =cut
97              
98             sub loglabel {
99 0     0 1   my $self = shift;
100 0           my $time = localtime();
101 0           return '[' . $time . '] ' . '[' . $self->nick . '] ';
102             }
103              
104             =head2 nick
105              
106             Returns pretty label unique to this DB object. Used by loglabel().
107              
108             =cut
109              
110             sub nick {
111 0     0 1   my $self = shift;
112 0   0       return join( '.',
113             $self->domain, $self->type, $self->database . '@' . ($self->host||'localhost') );
114             }
115              
116             =head2 dbi_connect
117              
118             Override base method to use DBI connect_cached() method.
119              
120             =cut
121              
122             sub dbi_connect {
123 0     0 1   my $self = shift;
124 0 0         $self->logger(" ---------> dbi->connect") if $self->debug;
125 0           return DBI->connect_cached(@_);
126             }
127              
128             1;
129              
130             __END__