my $genus = $schema->resultset('Genus')->find(12);
+ my $coderef2 = sub {
+ $genus->extinct(1);
+ $genus->update;
+ };
+
my $coderef1 = sub {
- my ($schema, $genus, $code) = @_;
$genus->add_to_species({ name => 'troglodyte' });
$genus->wings(2);
$genus->update;
- $schema->txn_do($code, $genus); # Can have a nested transaction
+ $schema->txn_do($coderef2); # Can have a nested transaction
return $genus->species;
};
- my $coderef2 = sub {
- my ($genus) = @_;
- $genus->extinct(1);
- $genus->update;
- };
-
my $rs;
eval {
- $rs = $schema->txn_do($coderef1, $schema, $genus, $coderef2);
+ $rs = $schema->txn_do($coderef1);
};
if ($@) { # Transaction failed
For example,
my $author_rs = $schema->resultset('Author')->find(1);
+ my @titles = qw/Night Day It/;
my $coderef = sub {
- my ($author, @titles) = @_;
-
# If any one of these fails, the entire transaction fails
- $author->create_related('books', {
+ $author_rs->create_related('books', {
title => $_
}) foreach (@titles);
my $rs;
eval {
- $rs = $schema->txn_do($coderef, $author_rs, qw/Night Day It/);
+ $rs = $schema->txn_do($coderef);
};
- if ($@) {
- my $error = $@;
- if ($error =~ /Rollback failed/) {
- die "something terrible has happened!";
- } else {
- deal_with_failed_transaction();
- }
+ if ($@) { # Transaction failed
+ die "something terrible has happened!" #
+ if ($@ =~ /Rollback failed/); # Rollback failed
+
+ deal_with_failed_transaction();
}
In a nested transaction (calling txn_do() from within a txn_do() coderef) only