Dancer-Plugin-MongoDB
view release on metacpan or search on metacpan
lib/Dancer/Plugin/MongoDB.pm view on Meta::CPAN
{
return $handle->{dbh};
} else {
if (_check_connection($handle->{dbh})) {
$handle->{last_connection_check} = time;
return $handle->{dbh};
} else {
$logger->(debug => "Database connection went away, reconnecting");
execute_hook('database_connection_lost', $handle->{dbh});
return $handle->{dbh}= _get_connection($conn_details);
}
}
} else {
# Get a new connection
if ($handle->{dbh} = _get_connection($conn_details)) {
$handle->{last_connection_check} = time;
$handles{$pid_tid}{$handle_key} = $handle;
if (ref $handle_key && ref $handle_key ne ref $def_handle) {
# We were given a hashref of connection settings. Shove a
# reference to that hashref into the handle, so that the hashref
# doesn't go out of scope for the life of the handle.
# Otherwise, that area of memory could be re-used, and, given
# different DB settings in a hashref that just happens to have
# the same address, we'll happily hand back the original handle.
# See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=665221
# Thanks to Sam Kington for suggesting this fix :)
$handle->{_orig_settings_hashref} = $handle_key;
}
return $handle->{dbh};
} else {
return;
}
}
};
register_hook(qw(mongodb_connected
mongodb_connection_lost
mongodb_connection_failed
mongodb_error));
register_plugin(for_versions => ['1', '2']);
# Given the settings to use, try to get a database connection
sub _get_connection {
my $settings = shift;
# Assemble the Connection String:
my $dsn = 'mongodb://' .
( $settings->{host} || 'localhost' ) .
( defined $settings->{port} ? ':' . $settings->{port} : () );
my $dbh = Mango->new($dsn);
$dbh->default_db($settings->{db_name})
if defined $settings->{db_name};
if (defined $settings->{username} && defined $settings->{password}) {
push @{$settings->{db_credentials}}, [ $settings->{db_name}, $settings->{username}, $settings->{password}];
}
if (defined $settings->{db_credentials} and ref $settings->{db_credentials} eq 'ARRAY') {
$dbh->credentials($settings->{db_credentials});
}
if (defined $settings->{ioloop}) {
my ( $module, $function ) = split(/\-\>/, $settings->{ioloop});
$dbh->ioloop($module->$function);
}
if (defined $settings->{j}) {
$dbh->j($settings->{j})
};
if (defined $settings->{max_bson_size}) {
$dbh->max_bson_size($settings->{max_bson_size})
};
if (defined $settings->{max_connections}) {
$dbh->max_connections($settings->{max_connections})
}
if (defined $settings->{max_write_batch_size}) {
$dbh->max_write_batch_size($settings->{max_write_batch_size})
}
if ( defined $settings->{protocol}) {
my ( $module, $function ) = split(/\-\>/, $settings->{protocol});
$dbh->protocol($module->$function);
}
if ( defined $settings->{w}) {
$dbh->w($settings->{w})
}
if ( defined $settings->{wtimeout}) {
$dbh->wtimeout($settings->{wtimeout})
}
#$dbh->on( error => \&_mango_error() );
#$dbh->on( connection => \&_mango_connection() );
if (!$dbh) {
$logger->(error => "Database connection failed - " . $lasterror);
execute_hook('database_connection_failed', $settings);
return;
}
execute_hook('database_connected', $dbh);
return $dbh;
}
# Check the connection is alive
sub _check_connection {
my $dbh = shift;
return unless $dbh;
my $curs;
$lasterror = undef;
eval {
lib/Dancer/Plugin/MongoDB.pm view on Meta::CPAN
# isn't ideal.)
if (!exists $return_settings->{connection_check_threshold}) {
$return_settings->{connection_check_threshold} = 30;
}
return $return_settings;
}
1;
__END__
=pod
=head1 VERSION
version 0.35
=head1 SYNOPSIS
use Dancer;
use Dancer::Plugin::MongoDB;
get '/widget/view/:id' => sub {
my $mg = mongo('mongoa');
my $db = $mg->database('foo');
my $cl = $db->collection('bar');
my $curs = $cl->find({ this => param('id') });
..
}
# or
get '/widget/view/:id' => sub {
my $mg = mongo('mongoa')->database('foo')->collection('bar')->find({ this => param('id') });
..
}
=head1 DESCRIPTION
Dancer::Plugin::MongoDB implements the "Mango" driver from the Mojolicious team. It
also uses some of the connection pooling features that the Dancer::Plugin::Database module
implements. For the most part, read the Mango documentation for full implementation.
=head1 CONFIGURATON
Connection details will be taken from your Dancer application config file, and
should be specified as follows:
plugins:
MongoDB:
host: "myhost"
port: 27017
db_name: "mydb"
username: "myuser"
password: "mypass"
w: 1
wtimeout: 1000
credentials:
[ mydb, myuser, mypass]
[ myotherdb, myotheruser, myotherpass]
or:
plugin:
MongoDB:
connections:
foohost:
host: "foohost"
port: 27017
db_name: "mydb"
barhost:
host: "barhost"
port: 27017
The attribute names are verbatim to the attribute names in Mango.
=head1 ACKNOWLEDGEMENTS
Thanks to Adam Taylor for the original Dancer::Plugin::Mongo.
Thanks to the Dancer team for creating a product that keeps me gainfully employed.
Thanks to the Mojolicious team for Mango.
This module is HEAVILY reliant on the original Dancer::Plugin::Database code. Most parts in here are unceremoniously copy-pasted from their code. Thanks guys for the work you're doing!
=head1 AUTHOR
Tyler Hardison <tyler@seraph-net.net>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Tyler Hardison.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
( run in 1.910 second using v1.01-cache-2.11-cpan-39bf76dae61 )