| 1 | package DBI::Transaction::Repeated; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw($VERSION); | ||
| 5 | |||
| 6 | $VERSION = 0.101; | ||
| 7 | |||
| 8 | use Exporter; | ||
| 9 | use vars qw(@ISA @EXPORT_OK); | ||
| 10 | push @ISA, 'Exporter'; | ||
| 11 | @EXPORT_OK = qw(&repeat_transaction | ||
| 12 | &ERR_OK &ERR_REPEAT &ERR_FAILED &ERR_REPEAT_EXCEEDED); | ||
| 13 | |||
| 14 | use DBI::Transaction; | ||
| 15 | |||
| 16 | use vars qw($MAX_REPEAT); | ||
| 17 | $MAX_REPEAT = 100; | ||
| 18 | |||
| 19 | use constant ERR_OK => {}; | ||
| 20 | use constant ERR_CANNOT_ROLL => {}; | ||
| 21 | use constant ERR_REPEAT => {}; | ||
| 22 | use constant ERR_DBI_FAILED => {}; | ||
| 23 | use constant ERR_FAILED => {}; | ||
| 24 | use constant ERR_REPEAT_EXCEEDED => {}; | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my $class = shift; | ||
| 28 | my $dbh = shift; | ||
| 29 | my $callback = shift; | ||
| 30 | my @named = @_; | ||
| 31 | |||
| 32 | my $self = { | ||
| 33 | DBH => $dbh, | ||
| 34 | CALLBACK => $callback, | ||
| 35 | _COUNT => $MAX_REPEAT, | ||
| 36 | _ARGS => [], | ||
| 37 | PrintDeadlockWarn => $dbh->{PrintWarn} | ||
| 38 | }; | ||
| 39 | die __PACKAGE__ . "::new: Wrong arguments." if @named % 2; | ||
| 40 | while(@named) { | ||
| 41 | my $id = shift @named; | ||
| 42 | die __PACKAGE__ . "::new: Wrong arguments." if $id !~ /^-/; | ||
| 43 | if($id eq '-count') { | ||
| 44 | $self->{_COUNT} = shift @named; | ||
| 45 | } elsif($id eq '-args') { | ||
| 46 | $self->{_ARGS} = shift @named; | ||
| 47 | } elsif($id eq '-PrintDeadlockErrors') { | ||
| 48 | $self->{PrintDeadlockErrors} = shift @named; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | return bless $self, $class; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub repeat_transaction { | ||
| 55 | my $class = UNIVERSAL::isa($_[0], __PACKAGE__) ? shift : __PACKAGE__; | ||
| 56 | |||
| 57 | return $class->new(@_)->run; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub step { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | my $dbh = $self->{DBH}; | ||
| 64 | |||
| 65 | my $T = DBI::Transaction->start($dbh); | ||
| 66 | return ERR_DBI_FAILED unless $T; | ||
| 67 | my $r = eval { &{$self->{CALLBACK}}($dbh, @{$self->{_ARGS}}) }; | ||
| 68 | local $dbh->{RaiseError} = 0; | ||
| 69 | #local $dbh->{PrintError} = 0; | ||
| 70 | if($r == ERR_REPEAT) { | ||
| 71 | $T->rollback | ||
| 72 | or # TODO: Broken API?.. | ||
| 73 | return UNIVERSAL::isa($dbh->errstr, "DBI::Transaction::CannotRollback") | ||
| 74 | ? ERR_CANNOT_ROLL : ERR_DBI_FAILED; | ||
| 75 | } elsif($r == ERR_OK) { | ||
| 76 | if($self->{_COUNT} != 1 && !$self->{PrintDeadlockWarn}) { | ||
| 77 | local $dbh->{PrintError} = 0; | ||
| 78 | return ERR_OK if $T->commit; | ||
| 79 | } else { | ||
| 80 | return ERR_OK if $T->commit; | ||
| 81 | } | ||
| 82 | } else { | ||
| 83 | if(!$T->rollback) { # Rollback failed counted as more serious error than callback failed | ||
| 84 | return UNIVERSAL::isa($dbh->errstr, "DBI::Transaction::CannotRollback") | ||
| 85 | ? ERR_CANNOT_ROLL : ERR_DBI_FAILED; | ||
| 86 | } | ||
| 87 | return ERR_FAILED; | ||
| 88 | } | ||
| 89 | |||
| 90 | return ERR_REPEAT unless $self->{_COUNT}; | ||
| 91 | --$self->{_COUNT}; | ||
| 92 | return $self->{_COUNT} ? ERR_REPEAT : ERR_REPEAT_EXCEEDED; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub run { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | my $err; | ||
| 99 | 1 while ($err = $self->step) == ERR_REPEAT; | ||
| 100 | return $err; | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; # DBI::Transaction::Repeated; | ||
| 104 | |||
| 105 | __END__ |