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:     @servers.each do |s|
53:       @available_connections[s] = []
54:       @allocated[s] = {}
55:     end
56:     @timeout = opts[:pool_timeout] || 5
57:     @sleep_time = opts[:pool_sleep_time] || 0.001
58:     @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true
59:   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 63
63:   def allocated(server=:default)
64:     @allocated[server]
65:   end

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

[Source]

    # File lib/sequel/connection_pool.rb, line 69
69:   def available_connections(server=:default)
70:     @available_connections[server]
71:   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 75
75:   def created_count(server=:default)
76:     @allocated[server].length + @available_connections[server].length
77:   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:       end
134:     end
135:   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 94
 94:   def hold(server=:default)
 95:     begin
 96:       t = Thread.current
 97:       time = Time.new
 98:       timeout = time + @timeout
 99:       sleep_time = @sleep_time
100:       if conn = owned_connection(t, server)
101:         return yield(conn)
102:       end
103:       begin
104:         until conn = acquire(t, server)
105:           raise(::Sequel::PoolTimeout) if Time.new > timeout
106:           sleep sleep_time
107:         end
108:         yield conn
109:       rescue Sequel::DatabaseDisconnectError => dde
110:         remove(t, conn, server) if conn
111:         raise
112:       ensure
113:         @mutex.synchronize{release(t, server)} if conn && !dde
114:       end
115:     rescue StandardError => e
116:       raise e
117:     rescue Exception => e
118:       raise(@convert_exceptions ? RuntimeError.new(e.message) : e)
119:     end
120:   end
size(server=:default)

Alias for created_count

[Validate]