1 package DBIx::Class::Storage::TxnScopeGuard;
5 use Carp::Clan qw/^DBIx::Class/;
8 my ($class, $storage) = @_;
11 bless [ 0, $storage ], ref $class || $class;
17 $self->[1]->txn_commit;
22 my ($dismiss, $storage) = @{$_[0]};
28 carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.'
31 my $rollback_exception;
34 eval { $storage->txn_rollback };
35 $rollback_exception = $@;
38 if ($rollback_exception && $rollback_exception !~ /DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION/) {
40 $@ = "Transaction aborted: ${exception} "
41 ."Rollback failed: ${rollback_exception}";
44 # throws an object (verified with eval{}) but DESTROY eats the exception
45 $storage->throw_exception ("Rollback failed: ${rollback_exception}");
56 DBIx::Class::Storage::TxnScopeGuard - Scope-based transaction handling
61 my ($self, $schema) = @_;
63 my $guard = $schema->txn_scope_guard;
65 # Multiple database operations here
72 An object that behaves much like L<Scope::Guard>, but hardcoded to do the
73 right thing with transactions in DBIx::Class.
79 Creating an instance of this class will start a new transaction (by
80 implicitly calling L<DBIx::Class::Storage/txn_begin>. Expects a
81 L<DBIx::Class::Storage> object as its only argument.
85 Commit the transaction, and stop guarding the scope. If this method is not
86 called and this object goes out of scope (i.e. an exception is thrown) then
87 the transaction is rolled back, via L<DBIx::Class::Storage/txn_rollback>
93 L<DBIx::Class::Schema/txn_scope_guard>.
99 Insipred by L<Scope::Guard> by chocolateboy.
101 This module is free software. It may be used, redistributed and/or modified
102 under the same terms as Perl itself.