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