From 847858bbf01e578c3bbced42ed05220a1af2bb66 Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Sun, 20 Dec 2015 12:21:38 -0800 Subject: [PATCH] transaction counter is per instance. The older transaction counter was for the whole class, which was problematic if you had more than one supporter database open at a time, or were otherwise using different supporter databases. I'm not completely sure this fix is fully functional, because perhaps we should be carrying this counter along with the DBH (i.e., what if two Supporter instances are created with the same dbh). I suppose we could fix this problem by changing the new() interface to require the instance itself be in control of the dbh. --- Supporters/lib/Supporters.pm | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index 03d94e9..32d44be 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -30,8 +30,6 @@ our $VERSION = '0.02'; use Scalar::Util qw(looks_like_number blessed); use Mail::RFC822::Address; -my $NESTED_TRANSACTION_COUNTER = 0; - ###################################################################### =begin new @@ -71,9 +69,10 @@ sub new ($$) { # begin_work/commit reference counter. $dbh->{RaiseError} = 0; $dbh->{HandleError} = sub { - $NESTED_TRANSACTION_COUNTER = 0; + $self->{__NESTED_TRANSACTION_COUNTER__} = 0; die $_[0]; }; + $self->{__NESTED_TRANSACTION_COUNTER__} = 0; return $self; } ###################################################################### @@ -687,11 +686,11 @@ This method is a reference counter to keep track of nested begin_work()/commit() sub _beginWork($) { my($self) = @_; - if ($NESTED_TRANSACTION_COUNTER < 0) { + if ($self->{__NESTED_TRANSACTION_COUNTER__} < 0) { die "_beginWork: Mismatched begin_work/commit pair in API implementation"; - $NESTED_TRANSACTION_COUNTER = 0; + $self->{__NESTED_TRANSACTION_COUNTER__} = 0; } - $self->dbh->begin_work() if ($NESTED_TRANSACTION_COUNTER++ == 0); + $self->dbh->begin_work() if ($self->{__NESTED_TRANSACTION_COUNTER__}++ == 0); } =item _commit() @@ -714,11 +713,11 @@ transactions to verify we don't nest $self->dbh->begin_work() sub _commit($) { my($self) = @_; - if ($NESTED_TRANSACTION_COUNTER < 0) { + if ($self->{__NESTED_TRANSACTION_COUNTER__} < 0) { die "_commit: Mismatched begin_work/commit pair in API implementation"; - $NESTED_TRANSACTION_COUNTER = 0; + $self->{__NESTED_TRANSACTION_COUNTER__} = 0; } - $self->dbh->commit() if (--$NESTED_TRANSACTION_COUNTER == 0); + $self->dbh->commit() if (--$self->{__NESTED_TRANSACTION_COUNTER__} == 0); } =item _rollback() @@ -740,7 +739,7 @@ This method resets the reference counter entirely and calls $dbh->rollback. sub _rollback($) { my($self) = @_; - $NESTED_TRANSACTION_COUNTER = 0; + $self->{__NESTED_TRANSACTION_COUNTER__} = 0; $self->dbh->rollback(); }