NAME
Scope::Container::DBI - DB connection manager with Scope::Container
SYNOPSIS
use Scope::Container::DBI;
use Scope::Container;
FOO: {
my $contaier = start_scope_container();
# first connect
my $dbh = Scope::Container::DBI->connect(
'dbi:mysql:mydb;host=myhost', 'myuser', 'mypasswd',
{ RaiseError => 1, mysql_connect_timeout => 4, mysql_enable_utf8 => 1 }
);
# same dsn, user/pass, and attributes, reuse connection
my $dbh2 = Scope::Container::DBI->connect(
'dbi:mysql:mydb;host=myhost', 'myuser', 'mypasswd',
{ RaiseError => 1, mysql_connect_timeout => 4, mysql_enable_utf8 => 1 }
);
#disconnect
}
BAR: {
my $contaier = start_scope_container();
# connect randomly
my $dbh = Scope::Container::DBI->connect(
['dbi:mysql:mydb;host=myslave01', 'myuser', 'mypasswd', {..}],
['dbi:mysql:mydb;host=myslave02', 'myuser', 'mypasswd', {..}],
['dbi:mysql:mydb;host=myslave03', 'myuser', 'mypasswd', {..}],
);
# reuse randomly connected
my $dbh2 = Scope::Container::DBI->connect(
['dbi:mysql:mydb;host=myslave01', 'myuser', 'mypasswd', {..}],
['dbi:mysql:mydb;host=myslave02', 'myuser', 'mypasswd', {..}],
['dbi:mysql:mydb;host=myslave03', 'myuser', 'mypasswd', {..}],
);
}
DESCRIPTION
Scope::Container::DBI is DB connection manager that uses Scope::Container. You can control DB connection within any scope.
METHOD
- $dbh = Scope::Container::DBI->connect();
-
connect to databases and cache connections.
$dbh = Scope::Container::DBI->connect($dsn,$user,$password,$attr);
You can give multiple dsn with arrayref, Scope::Container::DBI chooses database randomly.
$dbh = Scope::Container::DBI->connect( [$dsn,$user,$password,$attr], [$dsn,$user,$password,$attr], [$dsn,$user,$password,$attr] );
ADDITIONAL ATTRIBUTES
- ScopeContainerConnectRetry
-
number of connection retry, if failed connection.
my $dbh = Scope::Container::DBI->connect( 'dbi:mysql:mydb;host=myhost', 'myuser', 'mypasswd', { RaiseError => 1, mysql_connect_timeout => 4, ScopeContainerConnectRetry => 2 } );
If connection failed, Scope::Container::DBI retries 2 times internally.
- ScopeContainerConnectRetrySleep
-
millisecond. interval seconds of connection retry.
NOTE
- Fork/Thread Safety
-
Scope::Container::DBI checks pid or thread id when reuses database connections. If pid is different, sets InactiveDestroy to true and don't reuse it.
- Callbacks
-
Scope::Container::DBI doesn't have callback function, but you can set callbacks after connect with DBI's Callbacks function.
my $dbh = Scope::Container::DBI->connect($dsn, $username, $password, { RaiseError => 1, Callbacks => { connected => sub { shift->do(q{SET NAMES utf8}); }, }, });
- USING DBI SUBCLASSES
-
There is two way of using DBI subclass with Scope::Container::DBI. One is DBI's RootClass attribute, other is $Scope::Container::DBI::DBI_CLASS.
# use RootClass my $dbh = Scope::Container::DBI->connect($dsn, $username, $password, { RootClass => 'MySubDBI', }); # use $Scope::Container::DBI::DBI_CLASS local $Scope::Container::DBI::DBI_CLASS = 'MySubDBI'; my $dbh = Scope::Container::DBI->connect($dsn, $username, $password); # ref($dbh) is 'MySubDBI::db'
AUTHOR
Masahiro Nagano <kazeburo {at} gmail.com>
SEE ALSO
Scope::Container, Plack::Middleware::Scope::Container
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.