Skip to content

Commit aea2cd2

Browse files
committed
fix issue where dbh errcode gets lost when transaction fails to start
1 parent 6d4b1c5 commit aea2cd2

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{$NEXT}}
22
- wal_mode is now off by default, but can be enabled with the wal_mode option.
3+
- Fix issue where a transaction that failed to begin would rollback and clear the database handle error.
34

45
3.009 2022-03-22 20:16:03 EDT
56
- Fix race condition in Mojo::SQLite::Migrations where two processes could attempt to create the same migration. (#25, Oliver Kurz)

lib/Mojo/SQLite/Transaction.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ has db => undef, weak => 1;
1010
my %behaviors = map { ($_ => 1) } qw(deferred immediate exclusive);
1111

1212
sub new {
13-
my $self = shift->SUPER::new(@_, rollback => 1);
13+
my $self = shift->SUPER::new(@_);
1414
my $dbh = $self->{dbh} = $self->db->dbh;
1515
if (my $behavior = $self->{behavior}) {
1616
croak qq{Invalid transaction behavior $behavior} unless exists $behaviors{lc $behavior};
1717
$dbh->do("begin $behavior transaction");
1818
} else {
1919
$dbh->begin_work;
2020
}
21+
$self->{rollback} = 1;
2122
return $self;
2223
}
2324

t/results.t

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ my $sql = Mojo::SQLite->new;
148148
like $@, qr/does_not_exist/, 'right error';
149149
is_deeply $db->query('select * from results_test where name = ?', 'tx3')
150150
->hashes->to_array, [], 'no results';
151+
{
152+
my $timeout = $db->dbh->sqlite_busy_timeout // 0;
153+
$db->dbh->sqlite_busy_timeout(100);
154+
my $tx;
155+
eval {
156+
$tx = $db->begin('immediate');
157+
$db->begin('immediate');
158+
};
159+
ok $@, 'transaction failed';
160+
ok $db->dbh->err, 'database error code retained';
161+
$db->dbh->sqlite_busy_timeout($timeout);
162+
}
151163
};
152164

153165
subtest 'Issue #2' => sub {

0 commit comments

Comments
 (0)