-
-
Save hiroyukim/1710991 to your computer and use it in GitHub Desktop.
Amon2::Plugin::DBIx::Handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use utf8; | |
package Amon2::Plugin::DBIx::Handler; | |
use DBIx::Handler; | |
sub init { | |
my ($class, $context_class, $config) = @_; | |
no strict 'refs'; | |
*{"$context_class\::handler"} = \&_handler; | |
} | |
sub _handler { | |
my ($self) = @_; | |
if ( !defined $self->{handler} ) { | |
my $conf = $self->config->{'DBI'} | |
or die "missing configuration for 'DBI'"; | |
$self->{handler} = DBIx::Handler->new(@$conf); | |
} | |
return $self->{handler}; | |
} | |
1; | |
__END__ | |
in app.psgi or etc | |
__PACKAGE__->load_plugin('DBIx::Handler'); | |
my $txn = $c->handler->txn_scope; | |
# ここでトランザクションを効かせたい処理を行う | |
$c->handler->dbh->do('insert...'); | |
$txn->commit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
txn_scopeだけの用途なら
*{"DBIx::Handler:\txn"} = sub {
my ($self,$code) = @_;
try {
$self-> txn_scope
$code->();
} catch {
my $err = shift;
die $err;
};
$self->commit();
};