From f74c5d1854451eff2e3927d028a76237b77054fb Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Sun, 20 Dec 2015 18:52:08 -0800 Subject: [PATCH] DESTROY: detect _beginWork/_commit mismatches. The best place to detect these mismatches is in DESTROY. An error should be given when they are mismatched and we DESTROY the object. That's now done. --- Supporters/lib/Supporters.pm | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index e400af8..9db9f47 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -853,6 +853,32 @@ use outside of this module. =over +=item DESTROY + +=cut + +sub DESTROY { + my $self = shift; + return unless defined $self; + + # Force rollback if we somehow get destroy'ed while counter is up + if (defined $self->{__NESTED_TRANSACTION_COUNTER__} and $self->{__NESTED_TRANSACTION_COUNTER__} > 0) { + my $errorStr = "SUPPORTERS DATABASE ERROR: Mismatched begin_work/commit pair in API implementation"; + if (not defined $self->{dbh}) { + $errorStr .= "... and unable to rollback or commit work. Database may very well be inconsistent!"; + } else { + # Rollback if we didn't call commit enough; commit if we called commit too often. + ($self->{__NESTED_TRANSACTION_COUNTER__} > 0) ? $self->_rollback() : $self->_commit(); + $self->{dbh}->disconnect(); + } + $self->{__NESTED_TRANSACTION_COUNTER__} = 0; + die $errorStr; + } + delete $self->{__NESTED_TRANSACTION_COUNTER__}; + $self->{dbh}->disconnect() if defined $self->{dbh} and blessed($self->{dbh}) =~ /DBI/; +} + + =item _verifyId() Parameters: