Merge 'trunk' into 'cdbicompat_integration'
[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
21sub DESTROY {
22 my ($dismiss, $storage) = @{$_[0]};
23
3b7f3eac 24 return if $dismiss;
25
26 my $exception = $@;
27
28 $DB::single = 1;
29
30 local $@;
31 eval { $storage->txn_rollback };
32 my $rollback_exception = $@;
33 if($rollback_exception) {
34 my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION";
35
36 $storage->throw_exception(
37 "Transaction aborted: ${exception}. "
38 . "Rollback failed: ${rollback_exception}"
39 ) unless $rollback_exception =~ /$exception_class/;
40 }
1bc193ac 41}
42
431;
44
45__END__
46
47=head1 NAME
48
49DBIx::Class::Storage::TxnScopeGuard
50
51=head1 SYNOPSIS
52
53 sub foo {
54 my ($self, $schema) = @_;
55
56 my $guard = $schema->txn_scope_guard;
57
58 # Multiple database operations here
59
60 $guard->commit;
61 }
62
63=head1 DESCRIPTION
64
65An object that behaves much like L<Scope::Guard>, but hardcoded to do the
66right thing with transactions in DBIx::Class.
67
68=head1 METHODS
69
70=head2 new
71
72Creating an instance of this class will start a new transaction. Expects a
73L<DBIx::Class::Storage> object as its only argument.
74
75=head2 commit
76
77Commit the transaction, and stop guarding the scope. If this method is not
78called (i.e. an exception is thrown) and this object goes out of scope then
79the transaction is rolled back.
80
81=cut
82
83=head1 SEE ALSO
84
85L<DBIx::Class::Schema/txn_scope_guard>.
86
87=head1 AUTHOR
88
89Ash Berlin, 2008.
90
91Insipred by L<Scope::Guard> by chocolateboy.
92
93This module is free software. It may be used, redistributed and/or modified
94under the same terms as Perl itself.
95
96=cut