DBIx-Connector-Pool
view release on metacpan or search on metacpan
lib/DBIx/Connector/Pool.pm view on Meta::CPAN
}
$self->{wait_func}() if $self->{max_size} > 0 && @{$self->{pool}} >= $self->{max_size};
} while ($self->{max_size} > 0 && @{$self->{pool}} >= $self->{max_size});
my $connector = eval {DBIx::Connector::Pool::Item->new($self->{dsn}, $self->{user}, $self->{password}, $self->{attrs})};
if (!$connector) {
if ($self->{initial} > 0 && $self->{max_size} > $self->{initial}) {
--$self->{max_size};
carp "Corrected connector pool size: $self->{max_size}";
goto RESELECT;
} elsif ($self->{max_size} < 0) {
$self->{max_size} = @{$self->{pool}};
} else {
croak "Can't create new connector: " . DBI::errstr;
}
}
$connector->mode($self->{connector_mode});
$connector->used_now;
push @{$self->{pool}}, {tid => $tid, connector => $connector};
$connector;
}
1;
__END__
=head1 NAME
DBIx::Connector::Pool - A pool of DBIx::Connector or its subclasses for asynchronous environment
=head1 SYNOPSIS
use Coro;
use AnyEvent;
use Coro::AnyEvent;
use DBIx::Connector::Pool;
my $pool = DBIx::Connector::Pool->new(
initial => 1,
keep_alive => 1,
max_size => 5,
tid_func => sub {"$Coro::current" =~ /(0x[0-9a-f]+)/i; hex $1},
wait_func => sub {Coro::AnyEvent::sleep 0.05},
attrs => {RootClass => 'DBIx::PgCoroAnyEvent'}
);
async {
my $connector = $pool->get_connector;
$connector->run(
sub {
my $sth = $_->prepare(q{select isbn, title, rating from books});
$sth->execute;
my ($isbn, $title, $rating) = $sth->fetchrow_array;
# ...
}
);
};
=head1 Description
L<DBI> is great and L<DBIx::Connector> is a nice interface with good features
to it. But when it comes to work in some asynchronous environment like
L<AnyEvent> you have to use something another with callbacks if you don't want
to block your event loop completely waiting for data from DB. This module
(together with L<DBIx::PgCoroAnyEvent> for PostgreSQL or some another alike)
was developed to overcome this inconvenience. You can write your "normal" DBI
code without blocking your event loop.
This module requires some threading model and I know about only one really
working L<Coro>.
=head1 Methods
=over
=item B<new>
my $pool = DBIx::Connector::Pool->new(
initial => 1,
keep_alive => 1,
max_size => 5,
tid_func => sub {"$Coro::current" =~ /(0x[0-9a-f]+)/i; hex $1},
wait_func => sub {Coro::AnyEvent::sleep 0.05},
attrs => {RootClass => 'DBIx::PgCoroAnyEvent'}
);
Creates new pool. Possible parameters:
=over
=item B<initial>
Initial number of connected connectors. This means also minimum of of
connected connectors. It throws error if this minimum can not be met.
=item B<keep_alive>
How long connector can live after it becomes unused. Initial connectors will
live forever. C<-1> means no limit. C<0> means collect it immediate. Positive
number means seconds.
=item B<max_size>
Maximum pool capacity. C<-1> means unlimited.
=item B<user>
=item B<password>
=item B<dsn>
=item B<attrs>
Data for C<< DBIx::Connector->new >> function. This is the same as for
C<< DBI->connect >>. Usually you want to add some unblocking DBI subclass
as C<RootClass> attribute. Like C<< RootClass => 'DBIx::PgCoroAnyEvent' >>
for PostgreSQL.
=item B<connector_mode>
Sets the default L<DBIx::Connector/"Connection Modes"> attribute, which is
used by run(), txn(), and svp() if no mode is passed to them. Defaults to
"fixup".
( run in 1.507 second using v1.01-cache-2.11-cpan-140bd7fdf52 )