Activator
view release on metacpan or search on metacpan
lib/Activator/DB.pm view on Meta::CPAN
Connection and query debug dumps using your project or module level
C<Activator::Log> config, or on a per-query basis.
=item *
Allows all code in your project/team/company to access the db in a
consistent fashion.
=item *
By default, dies on all errors enforcing try/catch programming
=item *
Implemented as a singleton so each process is guranteed to be using no
more than one connection to each database from the pool.
=back
Disadvantages:
=over
=item *
If you know DBI, you don't necessarily know C<Activator::DB>
=item *
NOT THREAD SAFE
=item *
Only tested with MySql and PostgreSQL
=back
=head1 CONFIGURATION
This module uses L<Activator::Registry> to automatically choose default
databases, and L<Activator::Log> to log warnings and errors.
=head2 Registry Setup (from Activator::Registry)
This module expects an environment variable ACT_REG_YAML_FILE to be
set. If you are utilizing this module from apache, this directive must
be in your httpd configuration:
SetEnv ACT_REG_YAML_FILE '/path/to/config.yml'
If you are using this module from a script, you need to insure that
the environment is properly set using a BEGIN block:
BEGIN{
$ENV{ACT_REG_YAML_FILE} ||= '/path/to/config.yml'
}
=head2 Registry Configuration
Add an C<Activator::DB> section to your project YAML configuration file:
'Activator::Registry':
log4perl<.conf>: # Log4perl config file or definition
# See Logging Configuration below
'Activator::DB':
default: # default configuration for all connections
connection: <conn_alias>
## Optional default attributes and config for all connections
config:
debug: 0/1 # default: 0, affects all queries, all aliases
reconn_att: <int> # attempt reconnects this many times. default: 3
reconn_sleep: <int> # initial sleep seconds between reconnect attempts.
# doubles every attempt. default: 1
attr: # connection attributes. Only AutoCommit at this time
AutoCommit: 0/1 # default: 1
## You must define at least one connection alias
connections:
<conn_alias>:
user: <user>
pass: <password>
dsn: '<DSN>' # MySql Example: DBI:mysql:<DBNAME>:<DBHOST>
# PostgreSQL Example: DBI:Pg:dbname=<DBNAME>
# see: perldoc DBI, perldoc DBD::Pg, perldoc DBD::mysql
# for descriptions of valid DSNs
## These attributes and config are all optional, and use the default from above
attr:
AutoCommit: 0/1
config:
debug: 0/1 # only affects this connection
=head1 USAGE
This module can be used either pseudo-OO or static on multiple
databases. I say pseudo-OO, because you don't call new: this module
auto-vivicates a singleton object whenever you connect for the first
time.
=over
=item ## pseudo-OO example:
my $db = Activator::DB->connect( 'db_alias' );
$db->query_method( $sql, $bind, @args );
$db->connect( 'alt_db_alias' );
$db->query_method( $sql, $bind, @args );
$db->connect( 'db_alias' );
$db->query_method( $sql, $bind, @args );
=item ## Static formatted calls require that you dictate the connection for
every request. So, the above can also be done as:
Activator::DB->query_method( $sql, $bind, connect => 'db_alias', @args );
Activator::DB->query_method( $sql, $bind, connect => 'alt_db_alias', @args );
Activator::DB->query_method( $sql, $bind, connect => 'db_alias', @args );
=item ## However, the common use case for this module is:
my $db = Activator::DB->connect( 'db_alias' );
$db->query_method( $sql, $bind, @args );
### do some perl
$db->query_method( $sql, $bind, @args );
### do some perl
$db->query_method( $sql, $bind, @args );
### do some perl
... etc.
=back
=head2 connect() Usage
( run in 0.696 second using v1.01-cache-2.11-cpan-39bf76dae61 )