Skip to content

Instantly share code, notes, and snippets.

@hiroyukim
Forked from riywo/Handler.pm
Created January 31, 2012 15:12
Show Gist options
  • Save hiroyukim/1710991 to your computer and use it in GitHub Desktop.
Save hiroyukim/1710991 to your computer and use it in GitHub Desktop.
Amon2::Plugin::DBIx::Handler
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;
@hiroyukim
Copy link
Author

txn_scopeだけの用途なら

*{"DBIx::Handler:\txn"} = sub {
my ($self,$code) = @_;

try {
$self-> txn_scope
$code->();
} catch {
my $err = shift;
die $err;
};

$self->commit();
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment