File: connection_validator.rb [sequel-4.26.0 Documentation]

Last Modified
  2015-09-28 18:31:14 +0000
Requires

## Description

The connection_validator extension modifies a database's connection pool to validate that connections checked out from the pool are still valid, before yielding them for use. If it detects an invalid connection, it removes it from the pool and tries the next available connection, creating a new connection if no available connection is valid. Example of use:

    DB.extension(:connection_validator)

As checking connections for validity involves issuing a query, which is potentially an expensive operation, the validation checks are only run if the connection has been idle for longer than a certain threshold. By default, that threshold is 3600 seconds (1 hour), but it can be modified by the user, set to -1 to always validate connections on checkout:

    DB.pool.connection_validation_timeout = -1

Note that if you set the timeout to validate connections on every checkout, you should probably manually control connection checkouts on a coarse basis, using Database#synchronize. In a web application, the optimal place for that would be a rack middleware. Validating connections on every checkout without setting up coarse connection checkouts will hurt performance, in some cases significantly. Note that setting up coarse connection checkouts reduces the concurrency level acheivable. For example, in a web application, using Database#synchronize in a rack middleware will limit the number of concurrent web requests to the number to connections in the database connection pool.

Note that this extension only affects the default threaded and the sharded threaded connection pool. The single threaded and sharded single threaded connection pools are not affected. As the only reason to use the single threaded pools is for speed, and this extension makes the connection pool slower, there's not much point in modifying this extension to work with the single threaded pools. The threaded pools work fine even in single threaded code, so if you are currently using a single threaded pool and want to use this extension, switch to using a threaded pool.