Skip to content

Commit

Permalink
fix a bug that could cause connections to be cached for reuse too early
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 16, 2017
1 parent 63ddc21 commit f0b8341
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,6 +1,8 @@

4.04 2017-12-06
4.04 2017-12-16
- Added db attribute to Mojo::Pg::Results.
- Added sql_for method to Mojo::Pg::Migrations.
- Fixed a bug that could cause connections to be cached for reuse too early.

4.03 2017-11-04
- Improved Mojo::Pg::Database to use Mojo::Promise.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Pg/Database.pm
Expand Up @@ -106,7 +106,7 @@ sub query {
# Blocking
unless ($cb) {
$self->_notifications;
return $self->results_class->new(sth => $sth);
return $self->results_class->new(db => $self, sth => $sth);
}

# Non-blocking
Expand Down Expand Up @@ -172,7 +172,7 @@ sub _watch {
my $result = do { local $dbh->{RaiseError} = 0; $dbh->pg_result };
my $err = defined $result ? undef : $dbh->errstr;

$self->$cb($err, $self->results_class->new(sth => $sth));
$self->$cb($err, $self->results_class->new(db => $self, sth => $sth));
$self->_unwatch unless $self->{waiting} || $self->is_listening;
}
)->watch($self->{handle}, 1, 0);
Expand Down
9 changes: 8 additions & 1 deletion lib/Mojo/Pg/Results.pm
Expand Up @@ -5,7 +5,7 @@ use Mojo::Collection;
use Mojo::JSON 'from_json';
use Mojo::Util 'tablify';

has 'sth';
has [qw(db sth)];

sub DESTROY {
my $self = shift;
Expand Down Expand Up @@ -86,6 +86,13 @@ L<Mojo::Pg::Database>.
L<Mojo::Pg::Results> implements the following attributes.
=head2 db
my $db = $results->db;
$results = $results->db(Mojo::Pg::Database->new);
L<Mojo::Pg::Database> object these results belong to.
=head2 sth
my $sth = $results->sth;
Expand Down
14 changes: 13 additions & 1 deletion t/database.t
Expand Up @@ -121,6 +121,18 @@ isnt $db->query('select 5 as five')->sth, $sth, 'different statement handles';
isnt $db->query('select 6 as six')->sth, $sth, 'different statement handles';
is $db->query('select 3 as three')->sth, $sth, 'same statement handle';

# Connection reuse
$db = $pg->db;
$dbh = $db->dbh;
$results = $db->query('select 1');
undef $db;
my $db2 = $pg->db;
isnt $db2->dbh, $dbh, 'new database handle';
undef $results;
my $db3 = $pg->db;
is $db3->dbh, $dbh, 'same database handle';
is $db3->query('select 2')->array->[0], 2, 'right result';

# Dollar only
$db = $pg->db;
is $db->dollar_only->query('select $1::int as test', 23)->hash->{test}, 23,
Expand Down Expand Up @@ -199,7 +211,7 @@ isnt $db->dbh, $dbh, 'different database handle';
$db = $pg->db;
ok !$db->is_listening, 'not listening';
ok $db->listen('dbtest')->is_listening, 'listening';
my $db2 = $pg->db->listen('dbtest');
$db2 = $pg->db->listen('dbtest');
my @notifications;
Mojo::IOLoop->delay(
sub {
Expand Down

0 comments on commit f0b8341

Please sign in to comment.