# Transaction Methods
#####################################################################
+sub bz_in_transaction {
+ return $_[0]->{private_bz_transaction_count} ? 1 : 0;
+}
+
sub bz_start_transaction {
my ($self) = @_;
- if ($self->{private_bz_in_transaction}) {
- ThrowCodeError("nested_transaction");
+ if ($self->bz_in_transaction) {
+ $self->{private_bz_transaction_count}++;
} else {
# Turn AutoCommit off and start a new transaction
$self->begin_work();
- $self->{private_bz_in_transaction} = 1;
+ # REPEATABLE READ means "We work on a snapshot of the DB that
+ # is created when we execute our first SQL statement." It's
+ # what we need in Bugzilla to be safe, for what we do.
+ # Different DBs have different defaults for their isolation
+ # level, so we just set it here manually.
+ $self->do('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ');
+ $self->{private_bz_transaction_count} = 1;
}
}
sub bz_commit_transaction {
my ($self) = @_;
-
- if (!$self->{private_bz_in_transaction}) {
- ThrowCodeError("not_in_transaction");
- } else {
+
+ if ($self->{private_bz_transaction_count} > 1) {
+ $self->{private_bz_transaction_count}--;
+ } elsif ($self->bz_in_transaction) {
$self->commit();
-
- $self->{private_bz_in_transaction} = 0;
+ $self->{private_bz_transaction_count} = 0;
+ } else {
+ ThrowCodeError('not_in_transaction');
}
}
sub bz_rollback_transaction {
my ($self) = @_;
- if (!$self->{private_bz_in_transaction}) {
+ # Unlike start and commit, if we rollback at any point it happens
+ # instantly, even if we're in a nested transaction.
+ if (!$self->bz_in_transaction) {
ThrowCodeError("not_in_transaction");
} else {
$self->rollback();
-
- $self->{private_bz_in_transaction} = 0;
+ $self->{private_bz_transaction_count} = 0;
}
}
# above "die" condition.
$self->{RaiseError} = 1;
- # class variables
- $self->{private_bz_in_transaction} = 0;
-
bless ($self, $class);
return $self;
=over
+=item C<bz_in_transaction>
+
+Returns C<1> if we are currently in the middle of an uncommitted transaction,
+C<0> otherwise.
+
=item C<bz_start_transaction>
-Starts a transaction if supported by the database being used. Returns nothing
-and takes no parameters.
+Starts a transaction.
+
+It is OK to call C<bz_start_transaction> when you are already inside of
+a transaction. However, you must call L</bz_commit_transaction> as many
+times as you called C<bz_start_transaction>, in order for your transaction
+to actually commit.
+
+Bugzilla uses C<REPEATABLE READ> transactions.
+
+Returns nothing and takes no parameters.
=item C<bz_commit_transaction>
-Ends a transaction, commiting all changes, if supported by the database
-being used. Returns nothing and takes no parameters.
+Ends a transaction, commiting all changes. Returns nothing and takes
+no parameters.
=item C<bz_rollback_transaction>
-Ends a transaction, rolling back all changes, if supported by the database
-being used. Returns nothing and takes no parameters.
+Ends a transaction, rolling back all changes. Returns nothing and takes
+no parameters.
=back
ThrowCodeError("already_locked", { current => $self->{private_bz_tables_locked},
new => $list });
} else {
+ $self->bz_start_transaction();
$self->do('LOCK TABLE ' . $list);
-
$self->{private_bz_tables_locked} = $list;
}
}
sub bz_unlock_tables {
my ($self, $abort) = @_;
+
+ if ($self->bz_in_transaction) {
+ if ($abort) {
+ $self->bz_rollback_transaction();
+ }
+ else {
+ $self->bz_commit_transaction();
+ }
+ }
# Check first if there was previous matching lock
if (!$self->{private_bz_tables_locked}) {
ThrowCodeError("no_matching_lock");
} else {
$self->do("UNLOCK TABLES");
-
$self->{private_bz_tables_locked} = "";
}
}
-# As Bugzilla currently runs on MyISAM storage, which does not support
-# transactions, these functions die when called.
-# Maybe we should just ignore these calls for now, but as we are not
-# using transactions in MySQL yet, this just hints the developers.
-sub bz_start_transaction {
- die("Attempt to start transaction on DB without transaction support");
-}
-
-sub bz_commit_transaction {
- die("Attempt to commit transaction on DB without transaction support");
-}
-
-sub bz_rollback_transaction {
- die("Attempt to rollback transaction on DB without transaction support");
-}
-
-
sub _bz_get_initial_schema {
my ($self) = @_;
return $self->_bz_build_schema_from_disk();