« DBIx/Class/Storage/DBI/Cursor.pm
↑ Summary
DBIx/Class/Storage/DBI/Sybase/Microsoft_SQL_Server.pm »
| 1 | package DBIx::Class::Storage::TxnScopeGuard; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 267 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 267 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 267 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 267 | ▶ | use Scalar::Util qw(weaken blessed refaddr); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 267 | ▶ | use DBIx::Class; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 267 | ▶ | use DBIx::Class::_Util qw(is_exception detected_reinvoked_destructor); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 267 | ▶ | use DBIx::Class::Carp; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 267 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 8911 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 8911 | my ($class, $storage) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 8911 | 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 | 8911 | ▶ | if (is_exception $@) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | weaken( | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 591 | ▶ | $guard->{existing_exception_ref} = (ref($@) eq '') ? \$@ : $@ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 8911 | $storage->txn_begin; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 8911 | weaken( $guard->{dbh} = $storage->_dbh ); | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 8911 | ▶ | 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 | 8911 | $guard; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 8778 | ▶ | sub commit { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commit: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 8778 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | $self->{storage}->throw_exception("Refusing to execute multiple commits on scope guard $self") | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 8778 | ▶ | if $self->{inactivated}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # FIXME - this assumption may be premature: a commit may fail and a rollback | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # *still* be necessary. Currently I am not aware of such scenarious, but I | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # also know the deferred constraint handling is *severely* undertested. | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Making the change of "fire txn and never come back to this" in order to | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # address RT#107159, but this *MUST* be reevaluated later. | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 8778 | $self->{inactivated} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 8778 | $self->{storage}->txn_commit; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 8912 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=14 SCAR=26.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 8912 | ▶ | return if &detected_reinvoked_destructor; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 8911 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 8911 | ▶ | return if $self->{inactivated}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # if our dbh is not ours anymore, the $dbh weakref will go undef | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 133 | $self->{storage}->_verify_pid unless DBIx::Class::_ENV_::BROKEN_FORK; | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | 133 | ▶ | return unless $self->{dbh}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | my $exception = $@ if ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | is_exception $@ | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | and | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ! defined $self->{existing_exception_ref} | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | or | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | refaddr( ref($@) eq '' ? \$@ : $@ ) != refaddr($self->{existing_exception_ref}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 123 | ▶ | ) | ||||||||||||||||||||||||||||||||||||||||||||
|
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/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"})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 123 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 123 | ▶ | carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | unless defined $exception; | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 123 | my $rollback_exception; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | # do minimal connectivity check due to weird shit like | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # https://rt.cpan.org/Public/Bug/Display.html?id=62370 | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 123 | ▶ | try { $self->{storage}->_seems_connected && $self->{storage}->txn_rollback } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 123 | ▶ | catch { $rollback_exception = shift }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 123 | ▶ | 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')
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | ! defined blessed $rollback_exception | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | or | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | ! $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION') | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # append our text - THIS IS A TEMPORARY FIXUP! | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # a real stackable exception object is in the works | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 5 | ▶ | if (ref $exception eq 'DBIx::Class::Exception') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $exception->{msg} = "Transaction aborted: $exception->{msg} " | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | ."Rollback failed: ${rollback_exception}"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | elsif ($exception) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 3 | $exception = "Transaction aborted: ${exception} " | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | ."Rollback failed: ${rollback_exception}"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | carp (join ' ', | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | "********************* ROLLBACK FAILED!!! ********************", | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | "\nA rollback operation failed after the guard went out of scope.", | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 'This is potentially a disastrous situation, check your data for', | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | "consistency: $rollback_exception" | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 123 | $@ = $exception; | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 267 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||