class Sequel::ConnectionPool

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(Database, Hash)

Initialize using the passed Sequel::Database object and options hash.

hold(Symbol, &block)

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(Symbol)

Disconnect the connection object. For sharded connection pools, the Symbol passed is the shard/server to use.

servers

An array of shard/server symbols for all shards/servers that this connection pool recognizes.

size

an integer representing the total number of connections in the pool, or for the given shard/server if sharding is supported.

max_size

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:

add_servers(Array of Symbols)

start recognizing all shards/servers specified by the array of symbols.

remove_servers(Array of Symbols)

no longer recognize all shards/servers specified by the array of symbols.

Constants

OPTS
POOL_CLASS_MAP

Attributes

after_connect[RW]

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.

connect_sqls[RW]

An array of sql strings to execute on each new connection.

db[RW]

The Sequel::Database object tied to this connection pool.

Public Class Methods

new(db, opts=OPTS) click to toggle source

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:

:after_connect

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.

:connect_sqls

An array of sql strings to execute on each new connection, after :after_connect runs.

   # File lib/sequel/connection_pool.rb
93 def initialize(db, opts=OPTS)
94   @db = db
95   @after_connect = opts[:after_connect]
96   @connect_sqls = opts[:connect_sqls]
97   @error_classes = db.send(:database_error_classes).dup.freeze
98 end

Public Instance Methods

servers() click to toggle source

An array of symbols for all shards/servers, which is a single :default by default.

    # File lib/sequel/connection_pool.rb
101 def servers
102   [:default]
103 end

Private Instance Methods

disconnect_connection(conn) click to toggle source

Remove the connection from the pool. For threaded connections, this should be called without the mutex, because the disconnection may block.

    # File lib/sequel/connection_pool.rb
109 def disconnect_connection(conn)
110   db.disconnect_connection(conn)
111 end
disconnect_error?(exception) click to toggle source

Whether the given exception is a disconnect exception.

    # File lib/sequel/connection_pool.rb
114 def disconnect_error?(exception)
115   exception.is_a?(Sequel::DatabaseDisconnectError) || db.send(:disconnect_error?, exception, OPTS)
116 end
make_new(server) click to toggle source

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
120 def make_new(server)
121   begin
122     conn = @db.connect(server)
123 
124     if ac = @after_connect
125       if ac.arity == 2
126         ac.call(conn, server)
127       else
128         ac.call(conn)
129       end
130     end
131 
132     if cs = @connect_sqls
133       cs.each do |sql|
134         db.send(:log_connection_execute, conn, sql)
135       end
136     end
137   rescue Exception=>exception
138     raise Sequel.convert_exception_class(exception, Sequel::DatabaseConnectionError)
139   end
140   raise(Sequel::DatabaseConnectionError, "Connection parameters not valid") unless conn
141   conn
142 end