Class Sequel::ConnectionPool
In: lib/sequel/connection_pool.rb
Parent: Object

A ConnectionPool manages access to database connections by keeping multiple connections and giving threads exclusive access to each connection.

Methods

Attributes

connection_proc  [RW]  The proc used to create a new database connection.
disconnection_proc  [RW]  The proc used to disconnect a database connection.
max_size  [R]  The maximum number of connections.
mutex  [R]  The mutex that protects access to the other internal vairables. You must use this if you want to manipulate the variables safely.

Public Class methods

Constructs a new pool with a maximum size. If a block is supplied, it is used to create new connections as they are needed.

  pool = ConnectionPool.new(:max_connections=>10) {MyConnection.new(opts)}

The connection creation proc can be changed at any time by assigning a Proc to pool#connection_proc.

  pool = ConnectionPool.new(:max_connections=>10)
  pool.connection_proc = proc {MyConnection.new(opts)}

The connection pool takes the following options:

  • :disconnection_proc - The proc called when removing connections from the pool.
  • :max_connections - The maximum number of connections the connection pool will open (default 4)
  • :pool_convert_exceptions - Whether to convert non-StandardError based exceptions to RuntimeError exceptions (default true)
  • :pool_sleep_time - The amount of time to sleep before attempting to acquire a connection again (default 0.001)
  • :pool_timeout - The amount of seconds to wait to acquire a connection before raising a PoolTimeoutError (default 5)
  • :servers - A hash of servers to use. Keys should be symbols. If not present, will use a single :default server. The server name symbol will be passed to the connection_proc.

[Source]

    # File lib/sequel/connection_pool.rb, line 43
43:   def initialize(opts = {}, &block)
44:     @max_size = opts[:max_connections] || 4
45:     @mutex = Mutex.new
46:     @connection_proc = block
47:     @disconnection_proc = opts[:disconnection_proc]
48:     @servers = [:default]
49:     @servers += opts[:servers].keys - @servers if opts[:servers] 
50:     @available_connections = Hash.new{|h,k| h[:default]}
51:     @allocated = Hash.new{|h,k| h[:default]}
52:     @created_count = Hash.new{|h,k| h[:default]}
53:     @servers.each do |s|
54:       @available_connections[s] = []
55:       @allocated[s] = {}
56:       @created_count[s] = 0
57:     end
58:     @timeout = opts[:pool_timeout] || 5
59:     @sleep_time = opts[:pool_sleep_time] || 0.001
60:     @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true
61:   end

Public Instance methods

A hash of connections currently being used for the given server, key is the Thread, value is the connection.

[Source]

    # File lib/sequel/connection_pool.rb, line 65
65:   def allocated(server=:default)
66:     @allocated[server]
67:   end

An array of connections opened but not currently used, for the given server.

[Source]

    # File lib/sequel/connection_pool.rb, line 71
71:   def available_connections(server=:default)
72:     @available_connections[server]
73:   end

The total number of connections opened for the given server, should be equal to available_connections.length + allocated.length

[Source]

    # File lib/sequel/connection_pool.rb, line 77
77:   def created_count(server=:default)
78:     @created_count[server]
79:   end

Removes all connection currently available on all servers, optionally yielding each connection to the given block. This method has the effect of disconnecting from the database, assuming that no connections are currently being used. Once a connection is requested using hold, the connection pool creates new connections to the database.

[Source]

     # File lib/sequel/connection_pool.rb, line 127
127:   def disconnect(&block)
128:     block ||= @disconnection_proc
129:     @mutex.synchronize do
130:       @available_connections.each do |server, conns|
131:         conns.each{|c| block.call(c)} if block
132:         conns.clear
133:         set_created_count(server, allocated(server).length)
134:       end
135:     end
136:   end

Chooses the first available connection to the given server, or if none are available, creates a new connection. Passes the connection to the supplied block:

  pool.hold {|conn| conn.execute('DROP TABLE posts')}

Pool#hold is re-entrant, meaning it can be called recursively in the same thread without blocking.

If no connection is immediately available and the pool is already using the maximum number of connections, Pool#hold will block until a connection is available or the timeout expires. If the timeout expires before a connection can be acquired, a Sequel::PoolTimeout is raised.

[Source]

     # File lib/sequel/connection_pool.rb, line 96
 96:   def hold(server=:default)
 97:     begin
 98:       t = Thread.current
 99:       time = Time.new
100:       timeout = time + @timeout
101:       sleep_time = @sleep_time
102:       if conn = owned_connection(t, server)
103:         return yield(conn)
104:       end
105:       until conn = acquire(t, server)
106:         raise(::Sequel::PoolTimeout) if Time.new > timeout
107:         sleep sleep_time
108:       end
109:       begin
110:         yield conn
111:       rescue Sequel::DatabaseDisconnectError => dde
112:         remove(t, conn, server)
113:         raise
114:       ensure
115:         release(t, conn, server) unless dde
116:       end
117:     rescue Exception => e
118:       raise(@convert_exceptions && !e.is_a?(StandardError) ? RuntimeError.new(e.message) : e)
119:     end
120:   end
size(server=:default)

Alias for created_count

[Validate]