From: Justin Guenther Date: Thu, 25 May 2006 16:53:12 +0000 (+0000) Subject: Changed txn_do docs/Cookbook example to use closures, and made their content more... X-Git-Tag: v0.07002~83 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=70634260007b23e9d71c3962bb757b4532d76a02;p=dbsrgits%2FDBIx-Class.git Changed txn_do docs/Cookbook example to use closures, and made their content more consistent --- diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index 808c443..1cfed1d 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -409,24 +409,22 @@ example of the recommended way to use it: 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 diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 9105477..b130ff8 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -525,12 +525,11 @@ exception) an exception is thrown that includes a "Rollback failed" message. 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); @@ -539,16 +538,14 @@ For example, 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