File Coverage

blib/lib/KiokuDB/Backend/Role/TXN.pm
Criterion Covered Total %
statement 31 33 93.9
branch 7 14 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 46 55 83.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package KiokuDB::Backend::Role::TXN;
4 21     21   10857 use Moose::Role;
  21         37  
  21         166  
5              
6 21     21   90724 use Carp qw(croak);
  21         43  
  21         1070  
7 21     21   277 use Try::Tiny;
  21         29  
  21         1031  
8              
9 21     21   100 use namespace::clean -except => 'meta';
  21         31  
  21         159  
10              
11             requires qw(txn_begin txn_commit txn_rollback);
12              
13             sub txn_do {
14 2183     2183 1 5202 my ( $self, $coderef, %args ) = @_;
15              
16 2183 50       3226 my @args = @{ $args{args} || [] };
  2183         12430  
17              
18 2183         5761 my ( $commit, $rollback ) = @args{qw(commit rollback)};
19              
20 2183 50       6153 ref $coderef eq 'CODE' or croak '$coderef must be a CODE reference';
21              
22 2183         9683 my @txn_args = $self->txn_begin;
23              
24             try {
25 2183     2183   74789 my @ret;
26              
27 2183 50       5805 if ( wantarray ) {
    50          
28 0         0 @ret = $coderef->(@args);
29             } elsif ( defined wantarray ) {
30 0         0 $ret[0] = $coderef->(@args);
31             } else {
32 2183         7957 $coderef->(@args);
33             }
34              
35 1915 50       46749 $commit->() if $commit;
36 1915         18824 $self->txn_commit(@txn_args);
37              
38 1882 50       33762 return wantarray ? @ret : $ret[0];
39             } catch {
40 301     301   12232 my $err = $_;
41              
42             try {
43 301         12688 $self->txn_rollback(@txn_args);
44 268 50       5386 $rollback->() if $rollback;
45             } catch {
46 33         10528 croak "Transaction aborted: $err, rollback failed: $_";
47 301         2917 };
48              
49 268         10661 die $err;
50             }
51 2183         21458 }
52              
53             __PACKAGE__
54              
55             __END__
56              
57             =pod
58              
59             =head1 NAME
60              
61             KiokuDB::Backend::Role::TXN - Backend level transaction support.
62              
63             =head1 SYNOPSIS
64              
65             package MyBackend;
66             use Moose;
67              
68             with qw(
69             KiokuDB::Backend
70             KiokuDB::Backend::Role::TXN
71             );
72              
73             sub txn_begin { ... }
74             sub txn_commit { ... }
75             sub txn_rollback { ... }
76              
77             =head1 DESCRIPTION
78              
79             This API is inspired by standard database transactions much like you get with
80             L<DBI>.
81              
82             This is the low level interface required by L<KiokuDB/txn_do>.
83              
84              
85             =head1 OPTIONAL METHODS
86              
87             =over 4
88              
89             =item txn_do $code, %callbacks
90              
91             This method should evaluate the code reference in the context of a transaction,
92             inside an C<eval>. If any errors are caught the transaction should be aborted,
93             otherwise it should be committed. This is much like
94             L<DBIx::Class::Schema/txn_do>.
95              
96             The C<rollback> callback should be fired when the transaction will be aborted.
97              
98             =back
99              
100             =head1 REQUIRED METHODS
101              
102             =over 4
103              
104             =item txn_begin [ $parent_txn ]
105              
106             Begin a new transaction.
107              
108             This method can return a transaction handle that will later be passed to
109             C<txn_commit> or C<txn_rollback> as necessary.
110              
111             The current handle will be passed to nested calls to C<txn_begin>.
112              
113              
114             =item txn_commit $txn
115              
116             Commit the transaction.
117              
118             =item txn_rollback $txn
119              
120             Rollback the transaction.
121              
122             =back
123              
124             =cut