NAME
DBIx::PgLink::Roles::Reconnect - detect connection loss, reconnect and restart DBI call
DESCRIPTION
The role wraps some calls to DBI methods so that operation that fails due to connection break ( server shutdown, network troubles, etc), is automatically reconnected.
Failed operation will be repeated if AutoCommit mode on (no transaction). If transaction in progress than exception always raised, but connection has restored anyway.
REQUIREMENTS
DBI calls that needs protection must be executed via dbi_method
in base Adapter class.
Adapter class that does the role must implement following methods:
- is_disconnected
-
Must check supplied exception and return true value for disconnection error and false for any 'normal' database error.
- is_transaction_active
-
Must return true if there is transaction in progress.
Adapter class may implement following methods:
- always_valid_query
-
Return SQL query that always executed successfully ('SELECT 1').
- initialize_session
-
Restore database session state (SET options, create temporary tables, etc).
METHODS
- dbi_method
-
$self->dbi_method($handle, $func_name, @_)
Execute $func_name method of $handle object (DBI database or statement handle). Check for disconnection error, reconnect and retry the function (unless in transaction).
- check_connection_error
-
$self->check_connection_error($exception)
Check if $exception is disconnection error and try to reconnect. If not $exception specified, can execute
always_valid_query
SQL query to ensure the valid connection status. - reconnect
-
$self->reconnect
Try to reconnect
reconnect_retries
times withreconnect_interval
pause between attempts. Die if connection cannot be established or timed out.
ATTRIBUTES
- reconnect_retries
-
Max number of tries before giving up.
Default: 3
- reconnect_interval
-
Seconds to sleep after reconnection attempt fails
Default: 5
- reconnect_timeout
-
Timeout (in seconds) for waiting the database to accept connection.
Default: 5
CAVEATS
- All prepared statements are re-prepared at once, may cause heavy load
- Transaction retry does not implemented
TODO
Lazy prepare after reconnect (install subrole to Adapter::st)
Test reconnect timeout (SIGALRM) on different platforms
AUTHOR
Alexey Sharafutdinov <alexey.s.v.br@gmail.com>
Based on DBIx::RetryOverDisconnects module by Oleg Pronin <syber@cpan.org>
COPYRIGHT AND LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.