1 package DBIx::Class::Storage::TxnScopeGuard;
5 use Carp::Clan qw/^DBIx::Class/;
7 use Scalar::Util qw/weaken/;
11 my ($class, $storage) = @_;
14 my $guard = bless [ 0, $storage, $storage->_dbh ], ref $class || $class;
22 $self->[1]->txn_commit;
27 my ($dismiss, $storage) = @{$_[0]};
31 # if our dbh is not ours anymore, the weakref will go undef
32 $storage->_preserve_foreign_dbh;
33 return unless $_[0]->[2];
40 carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.'
43 my $rollback_exception;
44 # do minimal connectivity check due to weird shit like
45 # https://rt.cpan.org/Public/Bug/Display.html?id=62370
46 try { $storage->_seems_connected && $storage->txn_rollback }
47 catch { $rollback_exception = shift };
49 if (defined $rollback_exception && $rollback_exception !~ /DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION/) {
51 $exception = "Transaction aborted: ${exception} "
52 ."Rollback failed: ${rollback_exception}";
56 "********************* ROLLBACK FAILED!!! ********************",
57 "\nA rollback operation failed after the guard went out of scope.",
58 'This is potentially a disastrous situation, check your data for',
59 "consistency: $rollback_exception"
74 DBIx::Class::Storage::TxnScopeGuard - Scope-based transaction handling
79 my ($self, $schema) = @_;
81 my $guard = $schema->txn_scope_guard;
83 # Multiple database operations here
90 An object that behaves much like L<Scope::Guard>, but hardcoded to do the
91 right thing with transactions in DBIx::Class.
97 Creating an instance of this class will start a new transaction (by
98 implicitly calling L<DBIx::Class::Storage/txn_begin>. Expects a
99 L<DBIx::Class::Storage> object as its only argument.
103 Commit the transaction, and stop guarding the scope. If this method is not
104 called and this object goes out of scope (e.g. an exception is thrown) then
105 the transaction is rolled back, via L<DBIx::Class::Storage/txn_rollback>
111 L<DBIx::Class::Schema/txn_scope_guard>.
117 Inspired by L<Scope::Guard> by chocolateboy.
119 This module is free software. It may be used, redistributed and/or modified
120 under the same terms as Perl itself.