NAME
DBIx::DBHResolver - Pluggable library handles many databases a.k.a Database DBHResolver.
SYNOPSIS
use DBIx::DBHResolver;
DBIx::DBHResolver->config(+{
connect_info => +{
MASTER => +{
dsn => 'dbi:mysql:dbname=main;host=master',
user => 'root',
password => '',
attrs => +{ RaiseError => 1, AutoCommit => 0, }
},
SLAVE1 => +{
dsn => 'dbi:mysql:dbname=main;host=slave1',
user => 'root',
password => '',
attrs => +{ RaiseError => 1, AutoCommit => 0, }
},
SLAVE2 => +{
dsn => 'dbi:mysql:dbname=main;host=slave2',
user => 'root',
password => '',
attrs => +{ RaiseError => 1, AutoCommit => 0, }
},
HEAVY_MASTER1 => +{
dsn => 'dbi:mysql:dbname=heavy;host=heavy_master1',
user => 'root',
password => '',
attrs => +{ RaiseError => 1, AutoCommit => 0, }
},
HEAVY_MASTER2 => +{
dsn => 'dbi:mysql:dbname=heavy;host=heavy_master2',
user => 'root',
password => '',
attrs => +{ RaiseError => 1, AutoCommit => 0, }
},
},
cluster => +{
SLAVE => [ qw/SLAVE1 SLAVE2/ ],
HEAVY_MASTER => [ qw/HEAVY_MASTER1 HEAVY_MASTER2/ ]
},
});
my $master_conn_info = DBIx::DBHResolver->connect_info('MASTER');
my $master_dbh = DBIx::DBHResolver->connect('MASTER');
my ($even_num, $odd_num) = (100, 101);
### Using DBIx::DBHResolver::Strategy::Simple
my $heavy_cluster_list = DBIx::DBHResolver->cluster('HEAVY_MASTER');
my $heavy1_conn_info = DBIx::DBHResolver->connect_info('HEAVY_MASTER', +{ strategy => 'Simple', key => $even_num });
my $heavy2_dbh = DBIx::DBHResolver->connect_cached('HEAVY_MASTER', +{ strategy => 'Simple', key => $odd_num });
### Using DBIx::DBHResolver::Strategy::RoundRobin
my $slave_dbh = DBIx::DBHResolver->connect('SLAVE', +{ strategy => 'RoundRobin' });
DESCRIPTION
DBIx::DBHResolver is pluggable library handles many databases as known as Database DBHResolver Approach.
It can retrieve DBI's database handle object or connection information (data source, user, credential...) by labeled name using connect(), connect_cached(), connect_info() method, and treat same cluster consists many nodes as one labeled name, choose fetching strategy.
DBHResolver strategy is pluggable, so you can make custom strategy easily.
METHODS
load($yaml_file_path)
Load config file formatted yaml.
config(\%config)
Load config. (See SYNOPSIS)
connect($label, \%args)
Retrieve database handle. see below about \%args details.
- strategy
-
Specify strategy module name suffix. Default strategy module is prefixed 'DBIx::DBHResolver::Strategy::'. If you want to make custom strategy not prefixed 'DBIx::DBHResolver::Strategy::', add '+' prefixed module name such as '+MyApp::Strategy::Custom'.
- key
-
Strategy module uses hint choosing node.
connect_cached($label, \%args)
Retrieve database handle using DBI::connect_cached(). \%args is same as connect().
connect_info($label, \%args)
Retrieve connection info as HASHREF. \%args is same as connect().
cluster($cluster_name)
Retrieve cluster member node names as Array.
AUTHOR
Kosuke Arisawa <arisawa@gmail.com>
Toru Yamaguchi <zigorou@cpan.org>
SEE ALSO
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.