Skip to content

Commit

Permalink
Removed the _in_txn flag, since AutoCommit already has that info
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Sep 29, 2009
1 parent 8cb406f commit 45bb09f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
7 changes: 3 additions & 4 deletions lib/DBIx/Connection.pm
Expand Up @@ -132,7 +132,7 @@ sub do {

my @ret;
my $wantarray = wantarray;
if ($self->{_in_do} || $self->{_in_txn}) {
if ($self->{_in_do} || !$dbh->{AutoCommit}) {
@ret = _exec( $dbh, $code, $wantarray, @_);
return wantarray ? @ret : $ret[0];
}
Expand All @@ -157,13 +157,12 @@ sub txn_do {
my $wantarray = wantarray;
my @ret;

if ($self->{_in_txn}) {
unless ($dbh->{AutoCommit}) {
@ret = _exec( $dbh, $code, $wantarray, @_);
return $wantarray ? @ret : $ret[0];
}

local $self->{_in_do} = 1;
local $self->{_in_txn} = 1;

eval {
$dbh->begin_work;
Expand Down Expand Up @@ -192,7 +191,7 @@ sub svp_do {
my $dbh = $self->_dbh;

# Gotta have a transaction.
unless ($self->{_in_txn}) {
if ($dbh->{AutoCommit}) {
my @args = @_;
return $self->txn_do( sub { $self->svp_do($code, @args) } );
}
Expand Down
11 changes: 8 additions & 3 deletions t/do.t
Expand Up @@ -2,7 +2,7 @@

use strict;
use warnings;
use Test::More tests => 27;
use Test::More tests => 29;
#use Test::More 'no_plan';
use Test::MockModule;

Expand All @@ -18,9 +18,14 @@ ok my $conn = $CLASS->new( 'dbi:ExampleP:dummy', '', '' ),
my $module = Test::MockModule->new($CLASS);

# Test with no cached dbh.
$module->mock( _connect => '_connect');
$module->mock( _connect => sub {
pass '_connect should be called';
$module->original('_connect')->(@_);
});

ok $conn->do(sub {
is shift, '_connect', '_connect should have been called';
ok shift->{AutoCommit}, 'Inside, we should not be in a transaction';
ok $conn->{_in_do}, '_in_do should be true';
}), 'Do something with no cached handle';

# Test with cached dbh.
Expand Down
9 changes: 3 additions & 6 deletions t/svp_do.t
Expand Up @@ -2,7 +2,7 @@

use strict;
use warnings;
use Test::More tests => 34;
use Test::More tests => 31;
#use Test::More 'no_plan';
use Test::MockModule;

Expand All @@ -28,19 +28,16 @@ $module->mock( _connect => sub {
});

ok my $dbh = $conn->dbh, 'Fetch the database handle';
ok !$conn->{_in_txn}, 'We should not be in a txn';
ok !$conn->{_in_do}, '_in_do should be false';
ok $dbh->{AutoCommit}, 'AutoCommit should be true';
is $conn->{_svp_depth}, 0, 'Depth should be 0';

ok $conn->svp_do(sub {
ok !shift->{AutoCommit}, 'Inside, we should be in a transaction';
ok $conn->{_in_txn}, 'We should be in a transaction';
ok $conn->{_in_do}, '_in_do should be true';
is $conn->{_svp_depth}, 1, 'Depth should be 1';
}), 'Do something with no cached handle';
$module->unmock( '_connect');
ok !$conn->{_in_txn}, 'Transaction should be over';
ok !$conn->{_in_do}, '_in_do should be false again';
ok $dbh->{AutoCommit}, 'Transaction should be committed';
is $conn->{_svp_depth}, 0, 'Depth should be 0 again';
Expand Down Expand Up @@ -81,12 +78,12 @@ $conn->txn_do(sub {
# Make sure nested calls work.
$conn->svp_do(sub {
my $dbh = shift;
ok $conn->{_in_txn}, 'We should be in a txn';
ok !$dbh->{AutoCommit}, 'Inside, we should be in a transaction';
is $conn->{_svp_depth}, 1, 'Depth should be 1';
local $dbh->{Active} = 0;
$conn->svp_do(sub {
is shift, $dbh, 'Nested svp_do should get same dbh, even though inactive';
ok $conn->{_in_txn}, 'Nested txn_do should be in the txn';
ok !$dbh->{AutoCommit}, 'Nested txn_do should be in the txn';
is $conn->{_svp_depth}, 2, 'Depth should be 2';
});
is $conn->{_svp_depth}, 1, 'Depth should be 1 again';
Expand Down
12 changes: 5 additions & 7 deletions t/txn_do.t
Expand Up @@ -2,7 +2,7 @@

use strict;
use warnings;
use Test::More tests => 33;
use Test::More tests => 31;
#use Test::More 'no_plan';
use Test::MockModule;

Expand All @@ -24,17 +24,15 @@ $module->mock( _connect => sub {
});

ok my $dbh = $conn->dbh, 'Fetch the database handle';
ok !$conn->{_in_txn}, 'We should not be in a txn';
ok $dbh->{AutoCommit}, 'We should not be in a txn';
ok !$conn->{_in_do}, '_in_do should be false';
ok $dbh->{AutoCommit}, 'AutoCommit should be true';

ok $conn->txn_do(sub {
ok !shift->{AutoCommit}, 'Inside, we should be in a transaction';
ok $conn->{_in_txn}, 'We should be in a transaction';
ok !$conn->{AutoCommit}, 'We should be in a txn';
ok $conn->{_in_do}, '_in_do should be true';
}), 'Do something with no cached handle';
$module->unmock( '_connect');
ok !$conn->{_in_txn}, 'Transaction should be over';
ok !$conn->{_in_do}, '_in_do should be false again';
ok $dbh->{AutoCommit}, 'Transaction should be committed';

Expand Down Expand Up @@ -90,10 +88,10 @@ $conn->txn_do(sub {
# Make sure nested calls work.
$conn->txn_do(sub {
my $dbh = shift;
ok $conn->{_in_txn}, 'We should be in a txn';
ok !$conn->{AutoCommit}, 'We should be in a txn';
local $dbh->{Active} = 0;
$conn->txn_do(sub {
is shift, $dbh, 'Nested txn_do should get same dbh, even though inactive';
ok $conn->{_in_txn}, 'Nested txn_do should be in the txn';
ok !$conn->{AutoCommit}, 'Nested txn_do should be in the txn';
});
});

0 comments on commit 45bb09f

Please sign in to comment.