|
|
| |
Scope::Container::DBI(3) |
User Contributed Perl Documentation |
Scope::Container::DBI(3) |
Scope::Container::DBI - DB connection manager with Scope::Container
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', {..}],
);
}
Scope::Container::DBI is DB connection manager that uses Scope::Container. You
can control DB connection within any scope.
- $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]
);
- 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.
- 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'
Masahiro Nagano <kazeburo {at} gmail.com>
Scope::Container, Plack::Middleware::Scope::Container
This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. Output converted with ManDoc. |