1 package DBIx::Class::Storage::TxnScopeGuard;
5 use Scalar::Util qw(weaken blessed refaddr);
7 use DBIx::Class::_Util qw(is_exception detected_reinvoked_destructor);
12 my ($class, $storage) = @_;
19 # we are starting with an already set $@ - in order for things to work we need to
20 # be able to recognize it upon destruction - store its weakref
21 # recording it before doing the txn_begin stuff
23 # FIXME FRAGILE - any eval that fails but *does not* rethrow between here
24 # and the unwind will trample over $@ and invalidate the entire mechanism
25 # There got to be a saner way of doing this...
27 # Deliberately *NOT* using is_exception - if someone left a misbehaving
28 # antipattern value in $@, it's not our business to whine about it
29 if( defined $@ and length $@ ) {
31 $guard->{existing_exception_ref} = (length ref $@) ? $@ : \$@
37 weaken( $guard->{dbh} = $storage->_dbh );
39 bless $guard, ref $class || $class;
47 $self->{storage}->throw_exception("Refusing to execute multiple commits on scope guard $self")
48 if $self->{inactivated};
50 # FIXME - this assumption may be premature: a commit may fail and a rollback
51 # *still* be necessary. Currently I am not aware of such scenarious, but I
52 # also know the deferred constraint handling is *severely* undertested.
53 # Making the change of "fire txn and never come back to this" in order to
54 # address RT#107159, but this *MUST* be reevaluated later.
55 $self->{inactivated} = 1;
56 $self->{storage}->txn_commit;
60 return if &detected_reinvoked_destructor;
62 return if $_[0]->{inactivated};
64 # if our dbh is not ours anymore, the $dbh weakref will go undef
65 $_[0]->{storage}->_verify_pid unless DBIx::Class::_ENV_::BROKEN_FORK;
66 return unless $_[0]->{dbh};
68 my $current_exception = (
72 ! defined $_[0]->{existing_exception_ref}
74 refaddr( (length ref $@) ? $@ : \$@ ) != refaddr($_[0]->{existing_exception_ref})
84 carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.'
85 unless defined $current_exception;
87 my $rollback_exception;
88 # do minimal connectivity check due to weird shit like
89 # https://rt.cpan.org/Public/Bug/Display.html?id=62370
91 $_[0]->{storage}->_seems_connected && $_[0]->{storage}->txn_rollback;
93 } or $rollback_exception = $@;
95 if ( $rollback_exception and (
96 ! defined blessed $rollback_exception
98 ! $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
100 # append our text - THIS IS A TEMPORARY FIXUP!
101 # a real stackable exception object is in the works
102 if (ref $current_exception eq 'DBIx::Class::Exception') {
103 $current_exception->{msg} = "Transaction aborted: $current_exception->{msg} "
104 ."Rollback failed: ${rollback_exception}";
106 elsif ($current_exception) {
107 $current_exception = "Transaction aborted: ${current_exception} "
108 ."Rollback failed: ${rollback_exception}";
112 "********************* ROLLBACK FAILED!!! ********************",
113 "\nA rollback operation failed after the guard went out of scope.",
114 'This is potentially a disastrous situation, check your data for',
115 "consistency: $rollback_exception"
121 $@ = $current_exception;
130 DBIx::Class::Storage::TxnScopeGuard - Scope-based transaction handling
135 my ($self, $schema) = @_;
137 my $guard = $schema->txn_scope_guard;
139 # Multiple database operations here
146 An object that behaves much like L<Scope::Guard>, but hardcoded to do the
147 right thing with transactions in DBIx::Class.
153 Creating an instance of this class will start a new transaction (by
154 implicitly calling L<DBIx::Class::Storage/txn_begin>. Expects a
155 L<DBIx::Class::Storage> object as its only argument.
159 Commit the transaction, and stop guarding the scope. If this method is not
160 called and this object goes out of scope (e.g. an exception is thrown) then
161 the transaction is rolled back, via L<DBIx::Class::Storage/txn_rollback>
167 L<DBIx::Class::Schema/txn_scope_guard>.
169 L<Scope::Guard> by chocolateboy (inspiration for this module)
171 =head1 FURTHER QUESTIONS?
173 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
175 =head1 COPYRIGHT AND LICENSE
177 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
178 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
179 redistribute it and/or modify it under the same terms as the
180 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.