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

A SingleThreadedPool acts as a replacement for a ConnectionPool for use in single-threaded applications. ConnectionPool imposes a substantial performance penalty, so SingleThreadedPool is used to gain some speed.

Methods

conn   disconnect   hold   new  

Attributes

connection_proc  [W]  The proc used to create a new database connection
disconnection_proc  [RW]  The proc used to disconnect a database connection.

Public Class methods

Initializes the instance with the supplied block as the connection_proc.

The single threaded pool takes the following options:

  • :disconnection_proc - The proc called when removing connections from the pool.
  • :pool_convert_exceptions - Whether to convert non-StandardError based exceptions to RuntimeError exceptions (default true)
  • :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 221
221:   def initialize(opts={}, &block)
222:     @connection_proc = block
223:     @disconnection_proc = opts[:disconnection_proc]
224:     @conns = {}
225:     @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true
226:   end

Public Instance methods

The connection for the given server.

[Source]

     # File lib/sequel/connection_pool.rb, line 229
229:   def conn(server=:default)
230:     @conns[server]
231:   end

Disconnects from the database. Once a connection is requested using hold, the connection is reestablished.

[Source]

     # File lib/sequel/connection_pool.rb, line 252
252:   def disconnect(&block)
253:     block ||= @disconnection_proc
254:     @conns.values.each{|conn| block.call(conn) if block}
255:     @conns = {}
256:   end

Yields the connection to the supplied block for the given server. This method simulates the ConnectionPool#hold API.

[Source]

     # File lib/sequel/connection_pool.rb, line 235
235:   def hold(server=:default)
236:     begin
237:       begin
238:         yield(c = (@conns[server] ||= @connection_proc.call(server)))
239:       rescue Sequel::DatabaseDisconnectError => dde
240:         @conns.delete(server)
241:         @disconnection_proc.call(c) if @disconnection_proc
242:         raise
243:       end
244:     rescue Exception => e
245:       # if the error is not a StandardError it is converted into RuntimeError.
246:       raise(@convert_exceptions && !e.is_a?(StandardError) ? RuntimeError.new(e.message) : e)
247:     end
248:   end

[Validate]