Fix broken pod test (missing newline!)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / TxnScopeGuard.pm
CommitLineData
dd018f09 1package # Hide from pause for now - till we get it working
2 DBIx::Class::Storage::TxnScopeGuard;
1bc193ac 3
4use strict;
5use warnings;
6
7sub new {
8 my ($class, $storage) = @_;
9
10 $storage->txn_begin;
11 bless [ 0, $storage ], ref $class || $class;
12}
13
14sub commit {
15 my $self = shift;
16
17 $self->[1]->txn_commit;
18 $self->[0] = 1;
19}
20
a778f387 21sub dismiss {
22 my $self = shift;
23
24 $self->[0] = 1;
25}
26
1bc193ac 27sub DESTROY {
28 my ($dismiss, $storage) = @{$_[0]};
29
3b7f3eac 30 return if $dismiss;
31
32 my $exception = $@;
a778f387 33 Carp::cluck("A DBIx::Class:: went out of scope without explicit commit/dismiss")
34 unless $exception;
35 {
36 local $@;
37 eval { $storage->txn_rollback };
38 my $rollback_exception = $@;
39 if($rollback_exception) {
40 my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION";
41
42 $storage->throw_exception(
43 "Transaction aborted: ${exception}. "
44 . "Rollback failed: ${rollback_exception}"
45 ) unless $rollback_exception =~ /$exception_class/;
46 }
3b7f3eac 47 }
1bc193ac 48}
49
501;
51
52__END__
53
54=head1 NAME
55
56DBIx::Class::Storage::TxnScopeGuard
57
58=head1 SYNOPSIS
59
60 sub foo {
61 my ($self, $schema) = @_;
62
63 my $guard = $schema->txn_scope_guard;
64
65 # Multiple database operations here
66
67 $guard->commit;
68 }
69
70=head1 DESCRIPTION
71
72An object that behaves much like L<Scope::Guard>, but hardcoded to do the
73right thing with transactions in DBIx::Class.
74
75=head1 METHODS
76
77=head2 new
78
79Creating an instance of this class will start a new transaction. Expects a
80L<DBIx::Class::Storage> object as its only argument.
81
82=head2 commit
83
84Commit the transaction, and stop guarding the scope. If this method is not
85called (i.e. an exception is thrown) and this object goes out of scope then
86the transaction is rolled back.
87
88=cut
89
90=head1 SEE ALSO
91
92L<DBIx::Class::Schema/txn_scope_guard>.
93
94=head1 AUTHOR
95
96Ash Berlin, 2008.
97
98Insipred by L<Scope::Guard> by chocolateboy.
99
100This module is free software. It may be used, redistributed and/or modified
101under the same terms as Perl itself.
102
103=cut