[Bast-commits] r4102 - in DBIx-Class/0.08/branches/versioned_enhancements: . lib/DBIx/Class lib/DBIx/Class/Storage

captainL at dev.catalyst.perl.org captainL at dev.catalyst.perl.org
Tue Feb 26 13:16:22 GMT 2008


Author: captainL
Date: 2008-02-26 13:16:22 +0000 (Tue, 26 Feb 2008)
New Revision: 4102

Added:
   DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Storage/TxnScopeGuard.pm
Modified:
   DBIx-Class/0.08/branches/versioned_enhancements/
   DBIx-Class/0.08/branches/versioned_enhancements/Changes
   DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Row.pm
   DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Schema.pm
   DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Storage.pm
Log:
 r6807 at luke-mbp (orig r4050):  ash | 2008-02-10 17:31:27 +0000
 Add txn_scope_guard method/object



Property changes on: DBIx-Class/0.08/branches/versioned_enhancements
___________________________________________________________________
Name: svk:merge
   - 168d5346-440b-0410-b799-f706be625ff1:/DBIx-Class-current:2207
462d4d0c-b505-0410-bf8e-ce8f877b3390:/local/bast/DBIx-Class:3159
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/DBIx-Class/0.08/branches/on_disconnect_do:3694
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/DBIx-Class/0.08/trunk:4024
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-C3:318
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-current:2222
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-joins:173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-resultset:570
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/datetime:1716
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_compat:1855
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_unique_query_fixes:2142
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/inflate:1988
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/many_to_many:2025
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/re_refactor_bugfix:1944
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/reorganize_tests:1827
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset-new-refactor:1766
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_2_electric_boogaloo:2175
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_cleanup:2102
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/sqlt_tests_refactor:2043
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class:3606
fe160bb6-dc1c-0410-9f2b-d64a711b54a5:/local/DBIC-trunk-0.08:10510
   + 168d5346-440b-0410-b799-f706be625ff1:/DBIx-Class-current:2207
462d4d0c-b505-0410-bf8e-ce8f877b3390:/local/bast/DBIx-Class:3159
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/DBIx-Class/0.08/branches/on_disconnect_do:3694
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/DBIx-Class/0.08/trunk:4050
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-C3:318
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-current:2222
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-joins:173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-resultset:570
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/datetime:1716
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_compat:1855
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_unique_query_fixes:2142
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/inflate:1988
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/many_to_many:2025
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/re_refactor_bugfix:1944
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/reorganize_tests:1827
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset-new-refactor:1766
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_2_electric_boogaloo:2175
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_cleanup:2102
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/sqlt_tests_refactor:2043
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class:3606
fe160bb6-dc1c-0410-9f2b-d64a711b54a5:/local/DBIC-trunk-0.08:10510

Modified: DBIx-Class/0.08/branches/versioned_enhancements/Changes
===================================================================
--- DBIx-Class/0.08/branches/versioned_enhancements/Changes	2008-02-25 20:19:53 UTC (rev 4101)
+++ DBIx-Class/0.08/branches/versioned_enhancements/Changes	2008-02-26 13:16:22 UTC (rev 4102)
@@ -1,8 +1,8 @@
 Revision history for DBIx::Class
 
         - Versioning refactored
-
-0.08009 2008-01-20 13:30
+        - Created Storage::TxnScopeGuard object and txn_scope_guard methods
+          on Schema and Storage as an alternative way of doing transactions
         - Made search_rs smarter about when to preserve the cache to fix
           mm prefetch usage
         - Added Storage::DBI subclass for MSSQL over ODBC. 

Modified: DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Row.pm
===================================================================
--- DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Row.pm	2008-02-25 20:19:53 UTC (rev 4101)
+++ DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Row.pm	2008-02-26 13:16:22 UTC (rev 4102)
@@ -159,12 +159,10 @@
                        %{$self->{_inflated_column} || {}});
 
   if(!$self->{_rel_in_storage}) {
-    $source->storage->txn_begin;
 
     # The guard will save us if we blow out of this scope via die
+    $rollback_guard = $source->storage->txn_scope_guard;
 
-    $rollback_guard = Scope::Guard->new(sub { $source->storage->txn_rollback });
-
     ## Should all be in relationship_data, but we need to get rid of the
     ## 'filter' reltype..
     ## These are the FK rels, need their IDs for the insert.
@@ -246,8 +244,7 @@
         }
       }
     }
-    $source->storage->txn_commit;
-    $rollback_guard->dismiss;
+    $rollback_guard->commit;
   }
 
   $self->in_storage(1);

Modified: DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Schema.pm
===================================================================
--- DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Schema.pm	2008-02-25 20:19:53 UTC (rev 4101)
+++ DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Schema.pm	2008-02-26 13:16:22 UTC (rev 4102)
@@ -734,6 +734,15 @@
   $self->storage->txn_do(@_);
 }
 
+sub txn_scope_guard {
+  my $self = shift;
+
+  $self->storage or $self->throw_exception
+    ('txn_scope_guard called on $schema without storage');
+
+  $self->storage->txn_scope_guard(@_);
+}
+
 =head2 txn_begin
 
 Begins a transaction (does nothing if AutoCommit is off). Equivalent to

Added: DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Storage/TxnScopeGuard.pm
===================================================================
--- DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Storage/TxnScopeGuard.pm	                        (rev 0)
+++ DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Storage/TxnScopeGuard.pm	2008-02-26 13:16:22 UTC (rev 4102)
@@ -0,0 +1,79 @@
+package DBIx::Class::Storage::TxnScopeGuard;
+
+use strict;
+use warnings;
+
+sub new {
+  my ($class, $storage) = @_;
+
+  $storage->txn_begin;
+  bless [ 0, $storage ], ref $class || $class;
+}
+
+sub commit {
+  my $self = shift;
+
+  $self->[1]->txn_commit;
+  $self->[0] = 1;
+}
+
+sub DESTROY {
+  my ($dismiss, $storage) = @{$_[0]};
+
+  $storage->txn_rollback unless $dismiss;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+DBIx::Class::Storage::TxnScopeGuard
+
+=head1 SYNOPSIS
+
+ sub foo {
+   my ($self, $schema) = @_;
+
+   my $guard = $schema->txn_scope_guard;
+
+   # Multiple database operations here
+
+   $guard->commit;
+ }
+
+=head1 DESCRIPTION
+
+An object that behaves much like L<Scope::Guard>, but hardcoded to do the
+right thing with transactions in DBIx::Class. 
+
+=head1 METHODS
+
+=head2 new
+
+Creating an instance of this class will start a new transaction. Expects a
+L<DBIx::Class::Storage> object as its only argument.
+
+=head2 commit
+
+Commit the transaction, and stop guarding the scope. If this method is not
+called (i.e. an exception is thrown) and this object goes out of scope then
+the transaction is rolled back.
+
+=cut
+
+=head1 SEE ALSO
+
+L<DBIx::Class::Schema/txn_scope_guard>.
+
+=head1 AUTHOR
+
+Ash Berlin, 2008.
+
+Insipred by L<Scope::Guard> by chocolateboy.
+
+This module is free software. It may be used, redistributed and/or modified
+under the same terms as Perl itself.
+
+=cut

Modified: DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Storage.pm
===================================================================
--- DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Storage.pm	2008-02-25 20:19:53 UTC (rev 4101)
+++ DBIx-Class/0.08/branches/versioned_enhancements/lib/DBIx/Class/Storage.pm	2008-02-26 13:16:22 UTC (rev 4102)
@@ -8,6 +8,7 @@
 use Scalar::Util qw/weaken/;
 use Carp::Clan qw/^DBIx::Class/;
 use IO::File;
+use DBIx::Class::Storage::TxnScopeGuard;
 
 __PACKAGE__->mk_group_accessors('simple' => qw/debug debugobj schema/);
 __PACKAGE__->mk_group_accessors('inherited' => 'cursor_class');
@@ -261,6 +262,16 @@
 
 sub txn_rollback { die "Virtual method!" }
 
+=head2 txn_scope_guard
+
+Return an object that does stuff.
+
+=cut
+
+sub txn_scope_guard {
+  return DBIx::Class::Storage::TxnScopeGuard->new($_[0]);
+}
+
 =head2 sql_maker
 
 Returns a C<sql_maker> object - normally an object of class




More information about the Bast-commits mailing list