1 package DBIx::Class::Storage::TxnScopeGuard;
5 use Carp::Clan qw/^DBIx::Class/;
10 my ($class, $storage) = @_;
13 bless [ 0, $storage ], ref $class || $class;
19 $self->[1]->txn_commit;
24 my ($dismiss, $storage) = @{$_[0]};
33 carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.'
36 my $rollback_exception;
37 try { $storage->txn_rollback }
38 catch { $rollback_exception = shift };
40 if (defined $rollback_exception && $rollback_exception !~ /DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION/) {
42 $exception = "Transaction aborted: ${exception} "
43 ."Rollback failed: ${rollback_exception}";
47 "********************* ROLLBACK FAILED!!! ********************",
48 "\nA rollback operation failed after the guard went out of scope.",
49 'This is potentially a disastrous situation, check your data for',
50 "consistency: $rollback_exception"
65 DBIx::Class::Storage::TxnScopeGuard - Scope-based transaction handling
70 my ($self, $schema) = @_;
72 my $guard = $schema->txn_scope_guard;
74 # Multiple database operations here
81 An object that behaves much like L<Scope::Guard>, but hardcoded to do the
82 right thing with transactions in DBIx::Class.
88 Creating an instance of this class will start a new transaction (by
89 implicitly calling L<DBIx::Class::Storage/txn_begin>. Expects a
90 L<DBIx::Class::Storage> object as its only argument.
94 Commit the transaction, and stop guarding the scope. If this method is not
95 called and this object goes out of scope (e.g. an exception is thrown) then
96 the transaction is rolled back, via L<DBIx::Class::Storage/txn_rollback>
102 L<DBIx::Class::Schema/txn_scope_guard>.
108 Inspired by L<Scope::Guard> by chocolateboy.
110 This module is free software. It may be used, redistributed and/or modified
111 under the same terms as Perl itself.