DBD-Safe
view release on metacpan or search on metacpan
lib/DBD/Safe.pm view on Meta::CPAN
my $dbh = DBI->connect('DBI:Safe:', undef, undef, $dbd_safe_args);
All arguments for DBD::Safe are passed in the C<$dbd_safe_args> hashref.
This hashref can have following keys:
=over
=item I<dbi_connect_args>
It is an arrayref with arguments for C<< DBI->connect() >> which you pass when you
use DBI without DBD::Safe. These arguments will be used for (re)connection to
your database.
=item I<connect_cb>
Instead of passing C<dbi_connect_args> you can pass coderef that will be called
during (re)connection. This coderef must return database handler. Using
C<connect_cb> you can switch to another replica in case of disconnection or
implement another logic.
You must pass one of C<dbi_connect_args> or C<connect_cb>.
=item I<retry_cb>
This callback is used every time when DBD::Safe decides that reconnection needed.
By default DBD::Safe make only one try to reconnect and dies if it was
unsuccessful. You can override this using C<retry_cb>.
This callback takes one argument - number of reconnection trials - and returns
true or false (to make another reconnection attempt or not).
For example, you can place some C<sleep()> in this callback depending on number of trials.
=item I<reconnect_cb>
Callback that additionally checks if reconnection is necessary. Input argument is a C<$dbh>
handler, output - true or false.
For example, you can use this callback to reconnect every N seconds.
=back
=item C<x_safe_get_dbh>
If you have DBI with version >= 1.54, then you can explicitly call
my $real_dbh = $safe_dbh->x_safe_get_dbh;
This method will return real database handler that uses in the current time.
If you have DBI with version < 1.54, you can call
my $real_dbh = $safe_dbh->func('x_safe_get_dbh');
=back
=head1 BUGS AND CAVEATS
Connection is checked on each query. This can double your request execution time if all your requests are fast and network latency of your database is big enough.
Statement objects are not safe. Once you've prepared the statement, it won't reconnect to the database transparently.
There are no retries. If the request fails, it fails. This module just check that DB is alive *before* it tries to execute the statement. (Custom, per-query policies support is planned for the future releases).
=head1 SEE ALSO
L<http://github.com/tadam/DBD-Safe>,
L<DBIx::Connector>, L<DBIx::HA>, L<DBIx::DWIW>.
=head1 AUTHOR
Yury Zavarin <yury.zavarin@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Yury Zavarin.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
( run in 2.990 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )