blib/lib/DBIO/Storage/TxnScopeGuard.pm

filter: Statementstmt 96.8% 62 / 64 Branchbran 68.7% 22 / 32 Conditioncond 66.6% 12 / 18 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 83.6% 118 / 141 CC 23 SCAR 23.7
CC
23
Cov
84%
DESTROY
20.5
new
4.1
__ANON__
2.1
CRAP 10.7
« blib/lib/DBIO/Schema.pm ↑ Summary blib/lib/DBIO/Test/AsyncParity.pm »
1 package DBIO::Storage::TxnScopeGuard;
2 # ABSTRACT: Scope-based transaction handling
3  
4 345 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 345 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 345 our $VERSION = '0.900007';
8  
9 345 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 345 ▶use Scalar::Util qw(weaken blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 345 ▶use DBIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 345 ▶use DBIO::Util qw(is_exception is_windows);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 345 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 345 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 798 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
17 798 my ($class, $storage) = @_;
18  
19 798 my $guard = {
20 inactivated => 0,
21 storage => $storage,
22 };
23  
24 # we are starting with an already set $@ - in order for things to work we need to
25 # be able to recognize it upon destruction - store its weakref
26 # recording it before doing the txn_begin stuff
27 #
28 # FIXME FRAGILE - any eval that fails but *does not* rethrow between here
29 # and the unwind will trample over $@ and invalidate the entire mechanism
30 # There got to be a saner way of doing this...
31 798 ▶ if (is_exception $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_exception($@)) 5 793 798
32 weaken(
33 5 ▶ $guard->{existing_exception_ref} = (ref($@) eq '') ? \$@ : $@
Branch: 1 branch
BranchTrueFalseTotal
ref $@ eq '' ? : 0 5 5
34 );
35 }
36  
37 798 $storage->txn_begin;
38  
39 798 weaken( $guard->{dbh} = $storage->_dbh );
40  
41 798 ▶ bless $guard, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
07980
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
42  
43 798 $guard;
44 }
45  
46 766 ▶sub commit {
Subroutine commit: called CC=2 SCAR=6.9
Pod documented
47 766 my $self = shift;
48  
49 $self->{storage}->throw_exception("Refusing to execute multiple commit/rollbacks on scope guard $self")
50 766 ▶ if $self->{inactivated};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inactivated"} 1 765 766
51  
52 # A commit that fails ends the transaction: txn_commit resets the storage's
53 # transaction state, tries the rollback itself and rethrows the commit's
54 # error (karr k372). Nothing is left for the guard to roll back.
55 765 $self->{inactivated} = 1;
56 765 $self->{storage}->txn_commit;
57 }
58  
59 1 ▶sub rollback {
Subroutine rollback: called CC=2 SCAR=7.0
Pod documented
60 1 my $self = shift;
61  
62 $self->{storage}->throw_exception("Refusing to execute multiple commit/rollbacks on scope guard $self")
63 1 ▶ if $self->{inactivated};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inactivated"} 0 1 1
64  
65 1 $self->{inactivated} = 1;
66 1 $self->{storage}->txn_rollback;
67 }
68  
69 798 ▶sub DESTROY {
Subroutine DESTROY: called CC=17 SCAR=30.2
70 798 my $self = shift;
71  
72 798 ▶ if ($self->{_destroy_invoked}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_destroy_invoked"}++) 0 798 798
73 0 carp 'Preventing *MULTIPLE* DESTROY() invocations on DBIO::Storage::TxnScopeGuard';
74 0 return;
75 }
76  
77 798 ▶ return if $self->{inactivated};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inactivated"} 766 32 798
78 32 $self->{inactivated} = 1;
79  
80 # if our dbh is not ours anymore, the $dbh weakref will go undef
81 32 ▶ $self->{storage}->_verify_pid unless is_windows;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_windows() 32 0 32
82 32 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 32 32
83  
84 my $exception = $@ if (
85 is_exception $@
86 and
87 (
88 ! defined $self->{existing_exception_ref}
89 or
90 refaddr( ref($@) eq '' ? \$@ : $@ ) != refaddr($self->{existing_exception_ref})
91 32 ▶ )
Branch: 2 branches
BranchTrueFalseTotal
if &is_exception($@) and !defined($self->{"existing_exception_ref"}) || builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"}) 31 1 32
ref $@ eq '' ? : 0 0 0
Condition50%3/6
&is_exception($@) and !defined($self->{"existing_exception_ref"}) || builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
!ll&&!rl&&r
1031
not defined $self->{"existing_exception_ref"} or builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
l!l&&r!l&&!r
3100
MC/DC33%1/3
&is_exception($@) and !defined($self->{"existing_exception_ref"}) || builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
&is_exception($@) not defined $self->{"existing_exception_ref"} builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
Truth table50%2/4
A and not(B) or C
A= &is_exception($@)B= not defined $self->{"existing_exception_ref"}C= builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
ABCresult
0XX0
1000
11X1
1011
92 );
93  
94 {
95 32 local $@;
96  
97 32 ▶ carp 'A DBIO::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exception 1 31 32
98 unless defined $exception;
99  
100 32 my $rollback_exception;
101 # do minimal connectivity check due to weird shit like
102 # https://rt.cpan.org/Public/Bug/Display.html?id=62370
103 32 ▶ try { $self->{storage}->_seems_connected && $self->{storage}->txn_rollback }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$self->{"storage"}->_seems_connected and $self->{"storage"}->txn_rollback 32 0 32
104 32 ▶ catch { $rollback_exception = shift };
Subroutine __ANON__: called CC=1 SCAR=0.0
105  
106 32 ▶ if ( $rollback_exception and (
Branch: 1 branch
BranchTrueFalseTotal
if ($rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIO::Storage::NESTED_ROLLBACK_EXCEPTION")) 6 26 32
Condition83%5/6
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIO::Storage::NESTED_ROLLBACK_EXCEPTION")
!ll&&!rl&&r
2066
defined builtin::blessed($rollback_exception) && $rollback_exception->isa('DBIO::Storage::NESTED_ROLLBACK_EXCEPTION')
!ll&&!rl&&r
606
MC/DC66%2/3
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIO::Storage::NESTED_ROLLBACK_EXCEPTION")
$rollback_exception defined builtin::blessed($rollback_exception) $rollback_exception->isa('DBIO::Storage::NESTED_ROLLBACK_EXCEPTION')
Truth table75%3/4
A and not(B && C)
A= $rollback_exceptionB= defined builtin::blessed($rollback_exception)C= $rollback_exception->isa('DBIO::Storage::NESTED_ROLLBACK_EXCEPTION')
ABCresult
0XX0
1110
10X1
1101
107 ! defined blessed $rollback_exception
108 or
109 ! $rollback_exception->isa('DBIO::Storage::NESTED_ROLLBACK_EXCEPTION')
110 ) ) {
111 # append our text - THIS IS A TEMPORARY FIXUP!
112 # a real stackable exception object is in the works
113 # DBIO::Exception::Server is what the synchronous HandleError throws for
114 # a refused statement (ADR 0062); it has the same {msg} and is ours
115 6 ▶ if (ref $exception eq 'DBIO::Exception'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exception eq 'DBIO::Exception' or ref $exception eq 'DBIO::Exception::Server') { } 2 4 6
Condition100%3/3
ref $exception eq "DBIO::Exception" or ref $exception eq "DBIO::Exception::Server"
l!l&&r!l&&!r
114
MC/DC100%2/2
ref $exception eq "DBIO::Exception" or ref $exception eq "DBIO::Exception::Server"
ref $exception eq "DBIO::Exception" ref $exception eq "DBIO::Exception::Server"
Truth table100%3/3
A or B
A= ref $exception eq "DBIO::Exception"B= ref $exception eq "DBIO::Exception::Server"
ABresult
1X1
011
000
116 or ref $exception eq 'DBIO::Exception::Server'
117 ) {
118 2 $exception->{msg} = "Transaction aborted: $exception->{msg} "
119 ."Rollback failed: ${rollback_exception}";
120 }
121 else {
122 # Nothing else takes the news along. The inside of a foreign object,
123 # or of a DBIO::Exception subclass, is not ours to write into, and
124 # what gets assigned to $@ below is replaced by the exception in
125 # flight once the stack has unwound. The warning always arrives.
126 4 ▶ carp (join ' ',
Branch: 1 branch
BranchTrueFalseTotal
$exception ? : 4 0 4
127 "********************* ROLLBACK FAILED!!! ********************",
128 "\nA rollback operation failed after the guard went out of scope.",
129 'This is potentially a disastrous situation, check your data for',
130 "consistency: $rollback_exception"
131 . ( $exception
132 ? "\nThe guard went out of scope while this exception was in flight: $exception"
133 : ''
134 )
135 );
136  
137 4 ▶ $exception = "Transaction aborted: ${exception} "
Branch: 1 branch
BranchTrueFalseTotal
if $exception 4 0 4
138 ."Rollback failed: ${rollback_exception}"
139 if $exception;
140 }
141 }
142 }
143  
144 32 $@ = $exception;
145 }
146  
147 345 1;
148  
149 __END__
« blib/lib/DBIO/Schema.pm ↑ Summary blib/lib/DBIO/Test/AsyncParity.pm »