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 224
224:   def initialize(opts={}, &block)
225:     @connection_proc = block
226:     @disconnection_proc = opts[:disconnection_proc]
227:     @conns = {}
228:     @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true
229:   end

Public Instance methods

The connection for the given server.

[Source]

     # File lib/sequel/connection_pool.rb, line 232
232:   def conn(server=:default)
233:     @conns[server]
234:   end

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

[Source]

     # File lib/sequel/connection_pool.rb, line 255
255:   def disconnect(&block)
256:     block ||= @disconnection_proc
257:     @conns.values.each{|conn| block.call(conn) if block}
258:     @conns = {}
259:   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 238
238:   def hold(server=:default)
239:     begin
240:       begin
241:         yield(c = (@conns[server] ||= @connection_proc.call(server)))
242:       rescue Sequel::DatabaseDisconnectError => dde
243:         @conns.delete(server)
244:         @disconnection_proc.call(c) if @disconnection_proc
245:         raise
246:       end
247:     rescue Exception => e
248:       # if the error is not a StandardError it is converted into RuntimeError.
249:       raise(@convert_exceptions && !e.is_a?(StandardError) ? RuntimeError.new(e.message) : e)
250:     end
251:   end

[Validate]