The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBD::Multi - Manage Multiple Data Sources with Failover and Load Balancing

SYNOPSIS

  use DBI;

  my $other_dbh = DBI->connect(...);

  my $dbh = DBI->connect( 'dbi:Multi:', undef, undef, {
      dsns => [ # in priority order
          10 => [ 'dbi:SQLite:read_one.db', '', '' ],
          10 => [ 'dbi:SQLite:read_two.db', '', '' ],
          20 => [ 'dbi:SQLite:master.db',   '', '' ],
          30 => $other_dbh,
      ],
      # optional
      failed_max    => 1,     # short credibility
      failed_expire => 60*60, # long memory
  });

DESCRIPTION

This software manages multiple database connections for the purposes of load balancing and simple failover procedures. It acts as a proxy between your code and your available databases.

Although there is some code intended for read/write operations, this should be considered EXPIREMENTAL. This module is primary intended for read-only operations (where some other application is being used to handle replication). This software does not prevent write operations from being executed. This is left up to the user. (One suggestion is to make sure the user your a connecting to the db as has privileges sufficiently restricted to prevent updates).

The interface is nearly the same as other DBI drivers with one notable exception.

Configuring DSNs

Specify an attribute to the connect() constructor, dsns. This is a list of DSNs to configure. The configuration is given in pairs. First comes the priority of the DSN. Second is the DSN.

The priorities specify which connections should be used first (lowest to highest). As long as the lowest priority connection is responding, the higher priority connections will never be used. If multiple connections have the same priority, then one connection will be chosen randomly for each operation. Note that the random DB is chosen when the statement is prepared. Therefore executing multiple queries on the same prepared statement handle will always run on the same connection.

The second parameter can either be a DBI object or a list of parameters to pass to the DBI connect() instructor. If a set of parameters is given, then DBD::Multi will be able to attempt re-connect in the event that the connection is lost. If a DBI object is used, the DBD::Multi will give up permanently once that connection is lost.

Configuring Failures

By default a data source will not be tried again after it has failed three times. After five minutes that failure status will be removed and the data source may be tried again for future requests.

To change the maximum number of failures allowed before a data source is deemed failed, set the failed_max parameter. To change the amount of time we remember a data source as being failed, set the failed_expire parameter in seconds.

SEE ALSO

DBD::Multiplex, DBI, perl.

AUTHOR

Initially written by Casey West and Dan Wright for pair Networks, Inc. (www.pair.com)

Maintained by Dan Wright. <DWRIGHT@CPAN.ORG>.