Database-ManagedHandle
view release on metacpan or search on metacpan
lib/Database/ManagedHandle.pm view on Meta::CPAN
$cfg->{'dsn'} );
my $dbh = DBI->connect( $cfg->{'dsn'}, $cfg->{'username'}, $cfg->{'password'}, $cfg->{'attr'}, );
if ( !defined $dbh ) {
my $err = 'Could not open database. Error: %s';
croak sprintf $err, $DBI::errstr; ## no critic (Variables::ProhibitPackageVars)
}
return $dbh;
}
# This is partly borrowed from Dancer::Plugin::Database::Core and modified
# Check the connection is alive
sub _verify_connection_working {
my ( $self, $dbh ) = @_;
$self->_log->debugf( 'Verify connection working for handle \'%s\'', $dbh );
# If dbh is undef, obviously there is no connection.
return if ( !defined $dbh );
if ( $dbh->{Active} ) {
local $EVAL_ERROR = undef; # protect existing $@ ($EVAL_ERROR)
my $result = eval { $dbh->ping };
lib/Database/ManagedHandle.pm view on Meta::CPAN
needs to be defined as the default.
When the program first requests a database handle,
either a named handle or the default,
Database::ManagedHandle opens the database connection and
passes the opened handle to the program.
After using the handle, the program does not need to worry about it.
It can safely let the variable fall out of scope.
During subsequent calls for the handle, Database::ManagedHandle
first ensures that the connection is still alive.
If not, it will establish the handle again.
Do not keep a database handle around.
Only use the same handle for one operation, then purposefully undef it or let it
drop out of scope.
When you need it again, get it from Database::ManagedHandle.
=head2 Configuration
Database::ManagedHandle reads its configuration from
( run in 2.983 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )