Always return the same code ref for the same class.
David Wheeler
17 years ago
0 | 0 |
Revision history for Perl extension Exception::Class::DBI.
|
1 | 1 |
|
2 | 2 |
0.94
|
|
3 |
- The handler() method now always returns the same code referernce
|
|
4 |
each time it's called for a given subclass. This behavior prevents
|
|
5 |
the DBI connect_cached() method from caching a new database handle
|
|
6 |
every time it's called, which could otherwise be pretty annoying.
|
3 | 7 |
|
4 | 8 |
0.93 2006-06-10T02:09:14
|
5 | 9 |
- Reformatted some of the code and documentation so that it's
|
39 | 39 |
}
|
40 | 40 |
);
|
41 | 41 |
|
|
42 |
my %handlers;
|
42 | 43 |
sub handler {
|
43 | 44 |
my $pkg = shift;
|
|
45 |
return $handlers{$pkg} if $handlers{$pkg};
|
44 | 46 |
|
45 | 47 |
# Support subclasses.
|
46 | 48 |
my %class_for = map {
|
|
58 | 60 |
}
|
59 | 61 |
} qw(H DRH DBH STH Unknown);
|
60 | 62 |
|
61 | |
return sub {
|
|
63 |
return $handlers{$pkg} = sub {
|
62 | 64 |
my ($err, $dbh, $retval) = @_;
|
63 | 65 |
|
64 | 66 |
# No handle, no choice.
|
0 | 0 |
#!/usr/bin/perl -w
|
1 | 1 |
|
2 | 2 |
use strict;
|
3 | |
use Test::More tests => 9;
|
|
3 |
use Test::More tests => 12;
|
4 | 4 |
BEGIN { use_ok('Exception::Class::DBI') }
|
5 | 5 |
|
6 | 6 |
SUBCLASSES: {
|
|
24 | 24 |
}
|
25 | 25 |
|
26 | 26 |
use DBI;
|
|
27 |
|
|
28 |
# Make sure that the same handler is used every time.
|
|
29 |
is +MyApp::Ex::DBI->handler, MyApp::Ex::DBI->handler,
|
|
30 |
'The handler code ref should always be the same for the subclass';
|
|
31 |
is +Exception::Class::DBI->handler, Exception::Class::DBI->handler,
|
|
32 |
'The base class handler should always be the same code ref';
|
|
33 |
isnt +MyApp::Ex::DBI->handler, Exception::Class::DBI->handler,
|
|
34 |
'The subclass handler should be different from the base class handler';
|
27 | 35 |
|
28 | 36 |
ok my $dbh = DBI->connect('dbi:ExampleP:dummy', '', '', {
|
29 | 37 |
PrintError => 0,
|