The base connection pool class, which all other connection pools are based on. This class is not instantiated directly, but subclasses should at the very least implement the following API:
Initialize using the passed Sequel::Database object and options hash.
Yield a connection object (obtained from calling the block passed to
initialize
) to the current block. For sharded connection
pools, the Symbol passed is the shard/server
to use.
Disconnect the connection object. For sharded connection pools, the Symbol passed is the shard/server to use.
An array of shard/server symbols for all shards/servers that this connection pool recognizes.
an integer representing the total number of connections in the pool, or for the given shard/server if sharding is supported.
an integer representing the maximum size of the connection pool, or the maximum size per shard/server if sharding is supported.
For sharded connection pools, the sharded API adds the following methods:
start recognizing all shards/servers specified by the array of symbols.
no longer recognize all shards/servers specified by the array of symbols.
The #after_connect proc used for this pool. This is called with each new connection made, and is usually used to set custom per-connection settings.
An array of sql strings to execute on each new connection.
The Sequel::Database object tied to this connection pool.
Instantiates a connection pool with the given options. The block is called with a single symbol (specifying the server/shard to use) every time a new connection is needed. The following options are respected for all connection pools:
A callable object called after each new connection is made, with the connection object (and server argument if the callable accepts 2 arguments), useful for customizations that you want to apply to all connections.
An array of sql strings to execute on each new connection, after :after_connect runs.
Automatically create the maximum number of connections, so that they don't need to be created as needed. This is useful when connecting takes a long time and you want to avoid possible latency during runtime. Set to :concurrently to create the connections in separate threads. Otherwise they'll be created sequentially.
# File lib/sequel/connection_pool.rb, line 98 def initialize(db, opts=OPTS) @db = db @after_connect = opts[:after_connect] @connect_sqls = opts[:connect_sqls] @error_classes = db.send(:database_error_classes).dup.freeze end
An array of symbols for all shards/servers, which is a single
:default
by default.
# File lib/sequel/connection_pool.rb, line 106 def servers [:default] end
Remove the connection from the pool.
# File lib/sequel/connection_pool.rb, line 113 def disconnect_connection(conn) db.disconnect_connection(conn) end
Whether the given exception is a disconnect exception.
# File lib/sequel/connection_pool.rb, line 118 def disconnect_error?(exception) exception.is_a?(Sequel::DatabaseDisconnectError) || db.send(:disconnect_error?, exception, OPTS) end
Return a new connection by calling the connection proc with the given server name, and checking for connection errors.
# File lib/sequel/connection_pool.rb, line 124 def make_new(server) begin conn = @db.connect(server) if ac = @after_connect if ac.arity == 2 ac.call(conn, server) else ac.call(conn) end end if cs = @connect_sqls cs.each do |sql| db.send(:log_connection_execute, conn, sql) end end rescue Exception=>exception raise Sequel.convert_exception_class(exception, Sequel::DatabaseConnectionError) end raise(Sequel::DatabaseConnectionError, "Connection parameters not valid") unless conn conn end