1 package DBIx::Class::Storage::TxnScopeGuard;
6 use Scalar::Util qw/weaken blessed/;
8 use DBIx::Class::Exception;
12 my ($guards_count, $compat_handler, $foreign_handler);
15 my ($class, $storage) = @_;
18 my $guard = bless [ 0, $storage, $storage->_dbh ], ref $class || $class;
21 # install a callback carefully
22 if (DBIx::Class::_ENV_::INVISIBLE_DOLLAR_AT and !$guards_count) {
24 # if the thrown exception is a plain string, wrap it in our
26 # this is actually a pretty cool idea, may very well keep it
28 $compat_handler ||= bless(
30 $@ = (blessed($_[0]) or ref($_[0]))
32 : bless ( { msg => $_[0] }, 'DBIx::Class::Exception')
36 '__TxnScopeGuard__FIXUP__',
39 if ($foreign_handler = $SIG{__DIE__}) {
40 $SIG{__DIE__} = bless (
42 # we trust the foreign handler to do whatever it wants, all we do is set $@
43 eval { $compat_handler->(@_) };
44 $foreign_handler->(@_);
46 '__TxnScopeGuard__FIXUP__',
50 $SIG{__DIE__} = $compat_handler;
63 $self->[1]->txn_commit;
68 my ($dismiss, $storage) = @{$_[0]};
72 # don't touch unless it's ours, and there are no more of us left
74 DBIx::Class::_ENV_::INVISIBLE_DOLLAR_AT
79 if (ref $SIG{__DIE__} eq '__TxnScopeGuard__FIXUP__') {
80 # restore what we saved
81 if ($foreign_handler) {
82 $SIG{__DIE__} = $foreign_handler;
89 # make sure we do not leak the foreign one in case it exists
90 undef $foreign_handler;
95 # if our dbh is not ours anymore, the weakref will go undef
96 $storage->_verify_pid;
97 return unless $_[0]->[2];
104 carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.'
107 my $rollback_exception;
108 # do minimal connectivity check due to weird shit like
109 # https://rt.cpan.org/Public/Bug/Display.html?id=62370
110 try { $storage->_seems_connected && $storage->txn_rollback }
111 catch { $rollback_exception = shift };
113 if ( $rollback_exception and (
114 ! defined blessed $rollback_exception
116 ! $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
118 # append our text - THIS IS A TEMPORARY FIXUP!
119 # a real stackable exception object is in the works
120 if (ref $exception eq 'DBIx::Class::Exception') {
121 $exception->{msg} = "Transaction aborted: $exception->{msg} "
122 ."Rollback failed: ${rollback_exception}";
125 $exception = "Transaction aborted: ${exception} "
126 ."Rollback failed: ${rollback_exception}";
130 "********************* ROLLBACK FAILED!!! ********************",
131 "\nA rollback operation failed after the guard went out of scope.",
132 'This is potentially a disastrous situation, check your data for',
133 "consistency: $rollback_exception"
139 $@ = $exception unless DBIx::Class::_ENV_::INVISIBLE_DOLLAR_AT;
148 DBIx::Class::Storage::TxnScopeGuard - Scope-based transaction handling
153 my ($self, $schema) = @_;
155 my $guard = $schema->txn_scope_guard;
157 # Multiple database operations here
164 An object that behaves much like L<Scope::Guard>, but hardcoded to do the
165 right thing with transactions in DBIx::Class.
171 Creating an instance of this class will start a new transaction (by
172 implicitly calling L<DBIx::Class::Storage/txn_begin>. Expects a
173 L<DBIx::Class::Storage> object as its only argument.
177 Commit the transaction, and stop guarding the scope. If this method is not
178 called and this object goes out of scope (e.g. an exception is thrown) then
179 the transaction is rolled back, via L<DBIx::Class::Storage/txn_rollback>
185 L<DBIx::Class::Schema/txn_scope_guard>.
191 Inspired by L<Scope::Guard> by chocolateboy.
193 This module is free software. It may be used, redistributed and/or modified
194 under the same terms as Perl itself.