Commit | Line | Data |
6936e902 |
1 | package DBIx::Class::Storage::TxnScopeGuard; |
1bc193ac |
2 | |
3 | use strict; |
4 | use warnings; |
3d56e026 |
5 | use Scalar::Util qw(weaken blessed refaddr); |
b4ad8a74 |
6 | use DBIx::Class; |
e1d9e578 |
7 | use DBIx::Class::_Util qw(is_exception detected_reinvoked_destructor); |
70c28808 |
8 | use DBIx::Class::Carp; |
9c1700e3 |
9 | use namespace::clean; |
10 | |
1bc193ac |
11 | sub new { |
12 | my ($class, $storage) = @_; |
13 | |
f62c5724 |
14 | my $guard = { |
15 | inactivated => 0, |
16 | storage => $storage, |
17 | }; |
18 | |
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 |
6e102c8f |
22 | # |
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... |
35cf7d1a |
26 | # |
27 | # Deliberately *NOT* using is_exception - if someone left a misbehaving |
28 | # antipattern value in $@, it's not our business to whine about it |
d098704f |
29 | weaken( |
30 | $guard->{existing_exception_ref} = (length ref $@) ? $@ : \$@ |
31 | ) if( defined $@ and length $@ ); |
f62c5724 |
32 | |
1bc193ac |
33 | $storage->txn_begin; |
1f870d5a |
34 | |
6e102c8f |
35 | weaken( $guard->{dbh} = $storage->_dbh ); |
f62c5724 |
36 | |
37 | bless $guard, ref $class || $class; |
1f870d5a |
38 | |
7d216b10 |
39 | $guard; |
1bc193ac |
40 | } |
41 | |
42 | sub commit { |
43 | my $self = shift; |
44 | |
f62c5724 |
45 | $self->{storage}->throw_exception("Refusing to execute multiple commits on scope guard $self") |
46 | if $self->{inactivated}; |
47 | |
0bec44d5 |
48 | # FIXME - this assumption may be premature: a commit may fail and a rollback |
49 | # *still* be necessary. Currently I am not aware of such scenarious, but I |
50 | # also know the deferred constraint handling is *severely* undertested. |
51 | # Making the change of "fire txn and never come back to this" in order to |
52 | # address RT#107159, but this *MUST* be reevaluated later. |
f62c5724 |
53 | $self->{inactivated} = 1; |
0bec44d5 |
54 | $self->{storage}->txn_commit; |
1bc193ac |
55 | } |
56 | |
57 | sub DESTROY { |
e1d9e578 |
58 | return if &detected_reinvoked_destructor; |
3d56e026 |
59 | |
85944825 |
60 | return if $_[0]->{inactivated}; |
3b7f3eac |
61 | |
7d216b10 |
62 | |
84efb6d7 |
63 | # grab it before we've done volatile stuff below |
85944825 |
64 | my $current_exception = ( |
841efcb3 |
65 | is_exception $@ |
f62c5724 |
66 | and |
67 | ( |
85944825 |
68 | ! defined $_[0]->{existing_exception_ref} |
f62c5724 |
69 | or |
85944825 |
70 | refaddr( (length ref $@) ? $@ : \$@ ) != refaddr($_[0]->{existing_exception_ref}) |
f62c5724 |
71 | ) |
85944825 |
72 | ) |
73 | ? $@ |
74 | : undef |
75 | ; |
c6e27318 |
76 | |
84efb6d7 |
77 | |
78 | # if our dbh is not ours anymore, the $dbh weakref will go undef |
79 | $_[0]->{storage}->_verify_pid unless DBIx::Class::_ENV_::BROKEN_FORK; |
80 | return unless defined $_[0]->{dbh}; |
81 | |
82 | |
118b2c36 |
83 | carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back' |
84efb6d7 |
84 | unless defined $current_exception; |
85 | |
86 | |
87 | if ( |
88 | my $rollback_exception = $_[0]->{storage}->__delicate_rollback( |
89 | defined $current_exception |
90 | ? \$current_exception |
91 | : () |
92 | ) |
93 | and |
94 | ! defined $current_exception |
95 | ) { |
96 | carp (join ' ', |
97 | "********************* ROLLBACK FAILED!!! ********************", |
98 | "\nA rollback operation failed after the guard went out of scope.", |
99 | 'This is potentially a disastrous situation, check your data for', |
100 | "consistency: $rollback_exception" |
101 | ); |
3b7f3eac |
102 | } |
36099e8c |
103 | |
84efb6d7 |
104 | $@ = $current_exception |
105 | if DBIx::Class::_ENV_::UNSTABLE_DOLLARAT; |
d52fc26d |
106 | |
107 | # Dummy NEXTSTATE ensuring the all temporaries on the stack are garbage |
108 | # collected before leaving this scope. Depending on the code above, this |
109 | # may very well be just a preventive measure guarding future modifications |
110 | undef; |
1bc193ac |
111 | } |
112 | |
113 | 1; |
114 | |
115 | __END__ |
116 | |
117 | =head1 NAME |
118 | |
6936e902 |
119 | DBIx::Class::Storage::TxnScopeGuard - Scope-based transaction handling |
1bc193ac |
120 | |
121 | =head1 SYNOPSIS |
122 | |
123 | sub foo { |
124 | my ($self, $schema) = @_; |
125 | |
126 | my $guard = $schema->txn_scope_guard; |
127 | |
128 | # Multiple database operations here |
129 | |
130 | $guard->commit; |
131 | } |
132 | |
133 | =head1 DESCRIPTION |
134 | |
135 | An object that behaves much like L<Scope::Guard>, but hardcoded to do the |
fd323bf1 |
136 | right thing with transactions in DBIx::Class. |
1bc193ac |
137 | |
138 | =head1 METHODS |
139 | |
140 | =head2 new |
141 | |
6936e902 |
142 | Creating an instance of this class will start a new transaction (by |
143 | implicitly calling L<DBIx::Class::Storage/txn_begin>. Expects a |
1bc193ac |
144 | L<DBIx::Class::Storage> object as its only argument. |
145 | |
146 | =head2 commit |
147 | |
148 | Commit the transaction, and stop guarding the scope. If this method is not |
48580715 |
149 | called and this object goes out of scope (e.g. an exception is thrown) then |
6936e902 |
150 | the transaction is rolled back, via L<DBIx::Class::Storage/txn_rollback> |
1bc193ac |
151 | |
152 | =cut |
153 | |
154 | =head1 SEE ALSO |
155 | |
156 | L<DBIx::Class::Schema/txn_scope_guard>. |
157 | |
a2bd3796 |
158 | L<Scope::Guard> by chocolateboy (inspiration for this module) |
1bc193ac |
159 | |
a2bd3796 |
160 | =head1 FURTHER QUESTIONS? |
1bc193ac |
161 | |
a2bd3796 |
162 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. |
1bc193ac |
163 | |
a2bd3796 |
164 | =head1 COPYRIGHT AND LICENSE |
1bc193ac |
165 | |
a2bd3796 |
166 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> |
167 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can |
168 | redistribute it and/or modify it under the same terms as the |
169 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. |