Skip to content

Instantly share code, notes, and snippets.

@powerman
Created April 26, 2013 17:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save powerman/5468849 to your computer and use it in GitHub Desktop.
Save powerman/5468849 to your computer and use it in GitHub Desktop.
Using AnyEvent::DBI::MySQL with Mojolicious.
use AnyEvent::DBI::MySQL;
sub startup {
my $app = shift;
$app->config(db => {dsn=>…, login=>…, pass=>…});
$app->helper(dbh => sub { shift->{dbh} });
$app->helper(new_dbh => sub {
state $db = shift->app->config('db') or return;
return AnyEvent::DBI::MySQL->connect(@{$db}{qw(dsn login pass)},
{mysql_enable_utf8 => 1});
});
$app->hook(before_routes => sub {
my $c = shift;
# each connection have own dbh
$c->{dbh} = $c->new_dbh;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment