Commit | Line | Data |
6936e902 |
1 | package DBIx::Class::Storage::TxnScopeGuard; |
1bc193ac |
2 | |
3 | use strict; |
4 | use warnings; |
ed7ab0f4 |
5 | use Try::Tiny; |
f62c5724 |
6 | use Scalar::Util qw/weaken blessed refaddr/; |
b4ad8a74 |
7 | use DBIx::Class; |
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... |
f62c5724 |
26 | if (defined $@ and $@ ne '') { |
6e102c8f |
27 | weaken( |
28 | $guard->{existing_exception_ref} = (ref $@ ne '') ? $@ : \$@ |
29 | ); |
f62c5724 |
30 | } |
31 | |
1bc193ac |
32 | $storage->txn_begin; |
1f870d5a |
33 | |
6e102c8f |
34 | weaken( $guard->{dbh} = $storage->_dbh ); |
f62c5724 |
35 | |
36 | bless $guard, ref $class || $class; |
1f870d5a |
37 | |
7d216b10 |
38 | $guard; |
1bc193ac |
39 | } |
40 | |
41 | sub commit { |
42 | my $self = shift; |
43 | |
f62c5724 |
44 | $self->{storage}->throw_exception("Refusing to execute multiple commits on scope guard $self") |
45 | if $self->{inactivated}; |
46 | |
47 | $self->{storage}->txn_commit; |
48 | $self->{inactivated} = 1; |
1bc193ac |
49 | } |
50 | |
51 | sub DESTROY { |
f62c5724 |
52 | my $self = shift; |
1bc193ac |
53 | |
f62c5724 |
54 | return if $self->{inactivated}; |
3b7f3eac |
55 | |
f62c5724 |
56 | # if our dbh is not ours anymore, the $dbh weakref will go undef |
0d8817bc |
57 | $self->{storage}->_verify_pid unless DBIx::Class::_ENV_::BROKEN_FORK; |
f62c5724 |
58 | return unless $self->{dbh}; |
7d216b10 |
59 | |
f62c5724 |
60 | my $exception = $@ if ( |
61 | defined $@ |
62 | and |
63 | $@ ne '' |
64 | and |
65 | ( |
66 | ! defined $self->{existing_exception_ref} |
67 | or |
68 | refaddr( ref $@ eq '' ? \$@ : $@ ) != refaddr($self->{existing_exception_ref}) |
69 | ) |
70 | ); |
c6e27318 |
71 | |
a778f387 |
72 | { |
73 | local $@; |
36099e8c |
74 | |
75 | carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.' |
f62c5724 |
76 | unless defined $exception; |
36099e8c |
77 | |
ed7ab0f4 |
78 | my $rollback_exception; |
7d216b10 |
79 | # do minimal connectivity check due to weird shit like |
80 | # https://rt.cpan.org/Public/Bug/Display.html?id=62370 |
f62c5724 |
81 | try { $self->{storage}->_seems_connected && $self->{storage}->txn_rollback } |
ed7ab0f4 |
82 | catch { $rollback_exception = shift }; |
c6e27318 |
83 | |
90d7422f |
84 | if ( $rollback_exception and ( |
85 | ! defined blessed $rollback_exception |
86 | or |
87 | ! $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION') |
88 | ) ) { |
1f870d5a |
89 | # append our text - THIS IS A TEMPORARY FIXUP! |
90 | # a real stackable exception object is in the works |
91 | if (ref $exception eq 'DBIx::Class::Exception') { |
92 | $exception->{msg} = "Transaction aborted: $exception->{msg} " |
93 | ."Rollback failed: ${rollback_exception}"; |
94 | } |
95 | elsif ($exception) { |
36099e8c |
96 | $exception = "Transaction aborted: ${exception} " |
c6e27318 |
97 | ."Rollback failed: ${rollback_exception}"; |
36099e8c |
98 | } |
99 | else { |
100 | carp (join ' ', |
101 | "********************* ROLLBACK FAILED!!! ********************", |
102 | "\nA rollback operation failed after the guard went out of scope.", |
103 | 'This is potentially a disastrous situation, check your data for', |
104 | "consistency: $rollback_exception" |
105 | ); |
106 | } |
c6e27318 |
107 | } |
3b7f3eac |
108 | } |
36099e8c |
109 | |
5815ffb0 |
110 | $@ = $exception; |
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 | |
158 | =head1 AUTHOR |
159 | |
160 | Ash Berlin, 2008. |
161 | |
48580715 |
162 | Inspired by L<Scope::Guard> by chocolateboy. |
1bc193ac |
163 | |
164 | This module is free software. It may be used, redistributed and/or modified |
165 | under the same terms as Perl itself. |
166 | |
167 | =cut |