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[R]

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. Deprecated.

connect_sqls[R]

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

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 Database and options.

    # File lib/sequel/connection_pool.rb
104 def initialize(db, opts=OPTS) # SEQUEL6: Remove second argument, always use db.opts
105   @db = db
106   @use_old_connect_api = false # SEQUEL6: Remove
107   @after_connect = opts[:after_connect] # SEQUEL6: Remove
108   @connect_sqls = opts[:connect_sqls] # SEQUEL6: Remove
109   @error_classes = db.send(:database_error_classes).dup.freeze
110 end

Public Instance Methods

after_connect=(v) click to toggle source

Override the after_connect proc for the connection pool. Deprecated. Disables support for shard-specific :after_connect and :connect_sqls if used.

   # File lib/sequel/connection_pool.rb
85 def after_connect=(v) # SEQUEL6: Remove
86   @use_old_connect_api = true
87   @after_connect = v
88 end
connect_sqls=(v) click to toggle source

Override the connect_sqls for the connection pool. Deprecated. Disables support for shard-specific :after_connect and :connect_sqls if used.

   # File lib/sequel/connection_pool.rb
95 def connect_sqls=(v) # SEQUEL6: Remove
96   @use_old_connect_api = true
97   @connect_sqls = v
98 end
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
113 def servers
114   [:default]
115 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
121 def disconnect_connection(conn)
122   db.disconnect_connection(conn)
123 end
disconnect_error?(exception) click to toggle source

Whether the given exception is a disconnect exception.

    # File lib/sequel/connection_pool.rb
126 def disconnect_error?(exception)
127   exception.is_a?(Sequel::DatabaseDisconnectError) || db.send(:disconnect_error?, exception, OPTS)
128 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
132 def make_new(server)
133   begin
134     if @use_old_connect_api
135       # SEQUEL6: Remove block
136       conn = @db.connect(server)
137 
138       if ac = @after_connect
139         if ac.arity == 2
140           ac.call(conn, server)
141         else
142           ac.call(conn)
143         end
144       end
145 
146       if cs = @connect_sqls
147         cs.each do |sql|
148           db.send(:log_connection_execute, conn, sql)
149         end
150       end
151 
152       conn
153     else
154       @db.new_connection(server)
155     end
156   rescue Exception=>exception
157     raise Sequel.convert_exception_class(exception, Sequel::DatabaseConnectionError)
158   end || raise(Sequel::DatabaseConnectionError, "Connection parameters not valid")
159 end