| 1 | package DBIx::Class::Storage::TxnScopeGuard; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 251 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 251 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 251 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 251 | ▶ | use Scalar::Util qw/weaken blessed refaddr/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 251 | ▶ | use DBIx::Class; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 251 | ▶ | use DBIx::Class::_Util 'is_exception'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 251 | ▶ | use DBIx::Class::Carp; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 251 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 8593 | ▶ | my ($class, $storage) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 8593 | my $guard = { | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | inactivated => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | storage => $storage, | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # we are starting with an already set $@ - in order for things to work we need to | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # be able to recognize it upon destruction - store its weakref | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # recording it before doing the txn_begin stuff | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # FIXME FRAGILE - any eval that fails but *does not* rethrow between here | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # and the unwind will trample over $@ and invalidate the entire mechanism | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # There got to be a saner way of doing this... | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 8593 | ▶ | if (is_exception $@) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | weaken( | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 595 | ▶ | $guard->{existing_exception_ref} = (ref($@) eq '') ? \$@ : $@ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 8593 | $storage->txn_begin; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 8593 | weaken( $guard->{dbh} = $storage->_dbh ); | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 8593 | ▶ | bless $guard, ref $class || $class; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 8593 | $guard; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | sub commit { | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 8462 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commit: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | $self->{storage}->throw_exception("Refusing to execute multiple commits on scope guard $self") | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 8462 | ▶ | if $self->{inactivated}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 8462 | $self->{storage}->txn_commit; | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | 8462 | $self->{inactivated} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 8593 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=13 SCAR=26.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 8593 | ▶ | return if $self->{inactivated}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # if our dbh is not ours anymore, the $dbh weakref will go undef | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 131 | $self->{storage}->_verify_pid unless DBIx::Class::_ENV_::BROKEN_FORK; | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | 131 | ▶ | return unless $self->{dbh}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | my $exception = $@ if ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | is_exception $@ | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | and | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | ! defined $self->{existing_exception_ref} | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | or | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | refaddr( ref($@) eq '' ? \$@ : $@ ) != refaddr($self->{existing_exception_ref}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 121 | ▶ | ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
&is_exception($@) and !defined($self->{"existing_exception_ref"}) || builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
not defined $self->{"existing_exception_ref"} or builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
MC/DC0%0/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 table25%1/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"})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 121 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 121 | ▶ | carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | unless defined $exception; | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 121 | my $rollback_exception; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | # do minimal connectivity check due to weird shit like | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # https://rt.cpan.org/Public/Bug/Display.html?id=62370 | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 121 | ▶ | try { $self->{storage}->_seems_connected && $self->{storage}->txn_rollback } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 121 | ▶ | catch { $rollback_exception = shift }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 121 | ▶ | if ( $rollback_exception and ( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")
defined builtin::blessed($rollback_exception) && $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
MC/DC66%2/3
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")
$rollback_exception
defined builtin::blessed($rollback_exception)
$rollback_exception->isa('DBIx::Class::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('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | ! defined blessed $rollback_exception | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | or | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ! $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION') | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # append our text - THIS IS A TEMPORARY FIXUP! | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # a real stackable exception object is in the works | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 5 | ▶ | if (ref $exception eq 'DBIx::Class::Exception') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $exception->{msg} = "Transaction aborted: $exception->{msg} " | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | ."Rollback failed: ${rollback_exception}"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | elsif ($exception) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | $exception = "Transaction aborted: ${exception} " | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | ."Rollback failed: ${rollback_exception}"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | carp (join ' ', | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | "********************* ROLLBACK FAILED!!! ********************", | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | "\nA rollback operation failed after the guard went out of scope.", | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 'This is potentially a disastrous situation, check your data for', | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | "consistency: $rollback_exception" | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 121 | $@ = $exception; | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 251 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||