Class: Sequel::ConnectionPool

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/sequel/connection_pool.rb

Overview

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.

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.

Direct Known Subclasses

ShardedSingleConnectionPool, SingleConnectionPool, ThreadedConnectionPool

Defined Under Namespace

Modules: ClassMethods

Constant Summary

OPTS =
Sequel::OPTS
DEFAULT_SERVER =

The default server to use

:default
CONNECTION_POOL_MAP =

A map of [single threaded, sharded] values to symbols or ConnectionPool subclasses.

{[true, false] => :single, 
[true, true] => :sharded_single,
[false, false] => :threaded,
[false, true] => :sharded_threaded}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from ClassMethods

get_pool

Constructor Details

- (ConnectionPool) initialize(db, opts = OPTS)

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

The proc called after each new connection is made, with the connection object, useful for customizations that you want to apply to all connections.



75
76
77
78
# File 'lib/sequel/connection_pool.rb', line 75

def initialize(db, opts=OPTS)
  @db = db
  @after_connect = opts[:after_connect]
end

Instance Attribute Details

- (Object) after_connect

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.



63
64
65
# File 'lib/sequel/connection_pool.rb', line 63

def after_connect
  @after_connect
end

- (Object) db

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



66
67
68
# File 'lib/sequel/connection_pool.rb', line 66

def db
  @db
end

Instance Method Details

- (Object) created_count(*args)

Alias for size, not aliased directly for ease of subclass implementation



81
82
83
# File 'lib/sequel/connection_pool.rb', line 81

def created_count(*args)
  size(*args)
end

- (Object) servers

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



86
87
88
# File 'lib/sequel/connection_pool.rb', line 86

def servers
  [DEFAULT_SERVER]
end