| 1 | package DBIx::Fast::Transaction; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Object::Pad 0.807; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 8 | our $VERSION = '0.22'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Time::HiRes 'time'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 8 | DELAY => 1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | RETRIES => 3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | SLOW => 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 8 | my %VALID_ISOLATION = map { $_ => 1 } ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 'READ UNCOMMITTED', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 'READ COMMITTED', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 'REPEATABLE READ', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 'SERIALIZABLE', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | ▶ | class DBIx::Fast::Transaction :isa(DBIx::Fast::Base) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | field $level :accessor = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | field $savepoints :accessor = {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
level: called CC=2 SCAR=7.2
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | field $_sp_seq = 0; # monotonic savepoint sequence | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
savepoints: called CC=2 SCAR=7.1
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 8 | field $start_time :accessor = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | ▶ | field $stats :accessor = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start_time: called CC=2 SCAR=7.1
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | avg_duration => 0, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | deadlock_count => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | error_count => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | success_count => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | total_count => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | total_duration => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | max_duration => 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 42 | ▶ | method do ($code, $options = {}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do: called CC=19 SCAR=29.5
Subroutine
stats: called CC=2 SCAR=7.0
Pod
documented
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 42 | ▶ | $self->dbix->Exception("Transaction code block required") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | unless ref $code eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # Nested call: we are already inside a transaction owned by an outer do(). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # Run the code within that transaction and let the outermost do() own | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # begin/commit/rollback, retries and stats. Errors propagate to the outer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # handler (which performs the single rollback). Retry and isolation options | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # only take effect at the outermost level. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 40 | ▶ | if ($level > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | $level++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | my $nested = eval { $code->() }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | if (my $error = $@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | $level--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | die $error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | $level--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | return $nested; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 37 | ▶ | my $max_retries = $options->{max_retries} // RETRIES; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"max_retries"} // 3
MC/DC100%1/1
$options->{"max_retries"} // 3
$options->{"max_retries"}
Truth table100%2/2
A
A= $options->{"max_retries"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 37 | ▶ | my $retry_delay = $options->{retry_delay} // DELAY; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"retry_delay"} // 1
MC/DC100%1/1
$options->{"retry_delay"} // 1
$options->{"retry_delay"}
Truth table100%2/2
A
A= $options->{"retry_delay"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 37 | my $isolation = $options->{isolation}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 37 | ▶ | if (defined $isolation) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 10 | my $norm = uc $isolation; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 10 | $norm =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 10 | $norm =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | $self->dbix->Exception( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | "DBIx::Fast::Transaction - Invalid isolation level: $isolation") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 10 | ▶ | unless $VALID_ISOLATION{$norm}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 6 | $isolation = $norm; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 33 | $stats->{total_count}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # Isolation is applied once via SET SESSION (it persists for the whole | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # connection), so it survives across retries; restore the baseline once on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # exit. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 33 | ▶ | my $iso_baseline = defined $isolation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | ? $self->_apply_isolation($isolation) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 33 | my $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 33 | my $retries = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 33 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 35 | $start_time = time(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 35 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 35 | $self->_begin_work; # level == 0 at the outermost | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 35 | $level = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 35 | $result = $code->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 24 | $self->_commit; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 24 | $level = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # Drop this transaction's savepoints on success too (the failure path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # already does): a name left behind would let a later rollback_to() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # pass the exists() guard and fire a ROLLBACK TO on a savepoint the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # server no longer has, aborting the new transaction. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 24 | $savepoints = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 24 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 35 | ▶ | if ($ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 24 | $self->_update_stats(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 24 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 11 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # Determine retryability from the live handle BEFORE rolling back, since | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # the rollback itself resets $dbh->err. Also note whether the driver | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # reported this failure: if so, HandleError already recorded it in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # errors buffer and the generic "Transaction failed" entry would be a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # duplicate of the same event. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 11 | my $retryable = $self->_is_retryable($error); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 11 | my $db_reported = do { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 11 | my $dbh = eval { $self->dbix->db->dbh }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 11 | ▶ | $dbh && defined $dbh->err; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$dbh && defined $dbh->err
MC/DC50%1/2
$dbh && defined $dbh->err
$dbh
defined $dbh->err
Truth table66%2/3
A && B
A= $dbhB= defined $dbh->err
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # Unwind to a clean outermost state: roll back whatever is open, reset the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # nesting level and drop savepoints so the next attempt (or the caller) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # starts from scratch. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 11 | ▶ | eval { $self->_rollback() } if $level > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 11 | $level = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 11 | $savepoints = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 11 | ▶ | if ($retryable) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | $stats->{deadlock_count}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | ▶ | if ($retries < $max_retries) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | $retries++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | ▶ | sleep($retry_delay) if $retry_delay; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 9 | $self->_update_stats(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 9 | ▶ | $self->_restore_isolation($iso_baseline) if defined $iso_baseline; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 9 | ▶ | $self->dbix->set_error(500, "Transaction failed: $error") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | unless $db_reported; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 9 | die $error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 24 | ▶ | $self->_restore_isolation($iso_baseline) if defined $iso_baseline; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 24 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # A transaction is retryable when the failure is a deadlock or lock-wait | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | # timeout. Prefer the driver error code (read from the live handle before any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | # rollback); fall back to a free-text match so drivers without codes and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | # documented "deadlock" convention still trigger a retry. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 11 | ▶ | method _is_retryable ($error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_retryable: called CC=13 SCAR=28.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 11 | my $dbh = eval { $self->dbix->db->dbh }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 11 | my ($err, $state) = ('', ''); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 11 | ▶ | if ($dbh) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 11 | ▶ | $err = $dbh->err // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$dbh->err // ""
MC/DC100%1/1
$dbh->err // ""
$dbh->err
Truth table100%2/2
A
A= $dbh->err
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 11 | ▶ | $state = $dbh->state // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$dbh->state // ""
MC/DC0%0/1
$dbh->state // ""
$dbh->state
Truth table50%1/2
A
A= $dbh->state
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # MySQL/MariaDB: 1213 deadlock, 1205 lock wait timeout. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 11 | ▶ | return 1 if $err eq '1213' || $err eq '1205'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$err eq 1213 or $err eq 1205
MC/DC0%0/2
$err eq 1213 or $err eq 1205
$err eq 1213
$err eq 1205
Truth table33%1/3
A or B
A= $err eq 1213B= $err eq 1205
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # PostgreSQL SQLSTATE: 40P01 deadlock_detected, 40001 serialization_failure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 11 | ▶ | return 1 if $state eq '40P01' || $state eq '40001'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$state eq "40P01" or $state eq 40001
MC/DC0%0/2
$state eq "40P01" or $state eq 40001
$state eq "40P01"
$state eq 40001
Truth table33%1/3
A or B
A= $state eq "40P01"B= $state eq 40001
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # The driver reported a (non-retryable) error code: do NOT fall through to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # the free-text match. Driver messages echo user data ("Duplicate entry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # 'deadlock' for key ..."), which must not be able to force a retry loop. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # err 0 is DBI's "success with info" and S1000 its generic no-detail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # SQLSTATE; treat both as "no code". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 11 | ▶ | return 0 if ($err ne '' && $err ne '0') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition55%5/9
$err ne "" and $err ne 0 or $state ne "" and $state ne "S1000"
$err ne "" and $err ne 0
$state ne "" and $state ne "S1000"
MC/DC25%1/4
$err ne "" and $err ne 0 or $state ne "" and $state ne "S1000"
$err ne ""
$err ne 0
$state ne ""
$state ne "S1000"
Truth table28%2/7
A and B or C and D
A= $err ne ""B= $err ne 0C= $state ne ""D= $state ne "S1000"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | || ($state ne '' && $state ne 'S1000'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # No driver code at all: keep the documented convention that user code can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # die('deadlock ...') to request a retry. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 9 | ▶ | return 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | if $error =~ /deadlock|lock wait timeout|try restarting transaction/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 6 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 35 | ▶ | method _begin_work () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_begin_work: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 35 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 35 | my $dbh = $self->dbix->db->dbh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # With AutoCommit => 0 the handle is always inside an implicit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # transaction: begin_work would die "Already in a transaction", and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # commit/rollback already delimit the unit of work. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 35 | ▶ | $dbh->begin_work if $dbh->{AutoCommit}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 35 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | $self->dbix->set_error( 500, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | "Could not begin transaction: $@" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | die $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # Apply isolation level for the duration of one transaction. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # Returns the baseline level (driver-specific opaque string) so the caller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # can restore it after the transaction ends. Driver behavior: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # - MariaDB/mysql: SET SESSION (DBD::MariaDB does not honor SET TRANSACTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # before begin_work). Caller MUST restore baseline. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # - Pg: SET SESSION (consistent semantics). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # - SQLite: no-op (SQLite is effectively SERIALIZABLE always). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 6 | ▶ | method _apply_isolation ($level_name) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_isolation: called CC=9 SCAR=36.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 6 | ▶ | return undef unless defined $level_name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 6 | ▶ | return undef unless $VALID_ISOLATION{$level_name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 6 | my $driver = $self->dbix->dbd; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 6 | ▶ | return undef if $driver eq 'SQLite'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | my $dbh = $self->dbix->db->dbh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | my $baseline; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | if ($driver eq 'MariaDB' || $driver eq 'mysql') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$driver eq "MariaDB" or $driver eq "mysql"
MC/DC0%0/2
$driver eq "MariaDB" or $driver eq "mysql"
$driver eq "MariaDB"
$driver eq "mysql"
Truth table0%0/3
A or B
A= $driver eq "MariaDB"B= $driver eq "mysql"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ($baseline) = $dbh->selectrow_array('SELECT @@transaction_isolation'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # MariaDB reports as REPEATABLE-READ etc, normalize for SET syntax | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | $baseline =~ s/-/ /g if defined $baseline; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } elsif ($driver eq 'Pg') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ($baseline) = $dbh->selectrow_array('SHOW default_transaction_isolation'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $dbh->do($self->_isolation_set_sql($level_name)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | $self->dbix->set_error(500, "Could not set isolation level: $@"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | die $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | return $baseline; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # The driver-specific SQL to set the session isolation level. $level is always | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # a whitelist-validated value (see %VALID_ISOLATION), so interpolation is safe. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # Pg's session form is "SET SESSION CHARACTERISTICS AS TRANSACTION ..."; plain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # "SET SESSION TRANSACTION ISOLATION LEVEL" is a no-op (warning only) on Pg. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | method _isolation_set_sql ($level) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_isolation_set_sql: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | return $self->dbix->dbd eq 'Pg' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | ? "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL $level" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | : "SET SESSION TRANSACTION ISOLATION LEVEL $level"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | method _restore_isolation ($baseline) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_restore_isolation: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | return unless defined $baseline; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | return unless $VALID_ISOLATION{ uc $baseline }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | my $driver = $self->dbix->dbd; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | return if $driver eq 'SQLite'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | $self->dbix->db->dbh->do($self->_isolation_set_sql($baseline)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | # Restoration failure is non-fatal, but fail closed: drop the connection so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # the next dbh() call reconnects with default session state. Otherwise every | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | # later query on this handle would silently run at the transaction's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | # isolation level (e.g. READ UNCOMMITTED) for the life of the worker. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | $self->dbix->set_error(500, "Could not restore isolation level: $@"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | eval { $self->dbix->db->disconnect }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 24 | ▶ | method _commit () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_commit: called CC=2 SCAR=7.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 24 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 24 | $self->dbix->db->dbh->commit; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 24 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | $self->dbix->set_error( 500, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | "Could not commit transaction: $@" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | die $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 11 | ▶ | method _rollback () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rollback: called CC=2 SCAR=7.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 11 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 11 | $self->dbix->db->dbh->rollback; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 11 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | $self->dbix->set_error( 500, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | "Could not rollback transaction: $@" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | die $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 21 | ▶ | method savepoint ($name) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
savepoint: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 21 | ▶ | $self->dbix->Exception( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | "DBIx::Fast::Transaction - Savepoint name required") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | unless $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 18 | ▶ | $self->dbix->Exception( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | "DBIx::Fast::Transaction - Invalid savepoint name: $name") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | unless $name =~ /\A[a-zA-Z_][a-zA-Z0-9_]*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 13 | ▶ | unless ($level > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | $self->dbix->set_error(500, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | "savepoint('$name') requires an active transaction"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 12 | $savepoints->{$name} = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | level => $level, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | seq => ++$_sp_seq, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | time => time() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 12 | my $qname = $self->dbix->db->dbh->quote_identifier($name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 12 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 12 | $self->dbix->exec("SAVEPOINT $qname"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 12 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | delete $savepoints->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | $self->dbix->set_error( 500, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | "Could not create savepoint: $@" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | die $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 12 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 10 | ▶ | method rollback_to ($name) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rollback_to: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 10 | ▶ | $self->dbix->Exception( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | "DBIx::Fast::Transaction - Savepoint name required") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | unless $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 8 | ▶ | $self->dbix->Exception( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | "DBIx::Fast::Transaction - Invalid savepoint name: $name") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | unless $name =~ /\A[a-zA-Z_][a-zA-Z0-9_]*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 7 | ▶ | unless ( exists $savepoints->{$name} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 3 | $self->dbix->set_error( 500, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | "Savepoint '$name' does not exist" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 4 | my $qname = $self->dbix->db->dbh->quote_identifier($name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 4 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 4 | $self->dbix->exec("ROLLBACK TO SAVEPOINT $qname"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 4 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | $self->dbix->set_error( 500, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | "Could not rollback to savepoint: $@" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | die $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | # Drop every savepoint created after the target (higher sequence), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | # including same-level siblings, which the DB rolled back too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 4 | my $target_seq = $savepoints->{$name}->{seq}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 4 | for my $sp ( keys %{$savepoints} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | delete $savepoints->{$sp} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 7 | ▶ | if $savepoints->{$sp}->{seq} > $target_seq; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 4 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 33 | ▶ | method _update_stats ($success) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update_stats: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 33 | my $duration = time() - $start_time; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 33 | ▶ | if ($success) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 24 | $stats->{success_count}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 9 | $stats->{error_count}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 33 | $stats->{total_duration} += $duration; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | $stats->{max_duration} = $duration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 33 | ▶ | if $duration > $stats->{max_duration}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 33 | $stats->{avg_duration} = $stats->{total_duration} / $stats->{total_count}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 33 | ▶ | if ( $duration > SLOW ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | warn sprintf( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | "Slow transaction detected: %.2fs (threshold: %ds)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | $duration, SLOW ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 4 | ▶ | method get_stats () { return $stats } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_stats: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 4 | ▶ | method reset_stats () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_stats: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 4 | $stats = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | total_count => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | success_count => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | error_count => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | deadlock_count => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | total_duration => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | max_duration => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | avg_duration => 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 8 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||