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

new(opts={}, &block)

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 219
    219:   def initialize(opts={}, &block)
    220:     @connection_proc = block
    221:     @disconnection_proc = opts[:disconnection_proc]
    222:     @conns = {}
    223:     @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true
    224:   end

### Public Instance methods

conn(server=:default)

The connection for the given server.

[Source]

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

disconnect(&block)

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

[Source]

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

hold(server=:default) {|c = (@conns[server] ||= @connection_proc.call(server))| ...}

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 233
    233:   def hold(server=:default)
    234:     begin
    235:       begin
    236:         yield(c = (@conns[server] ||= @connection_proc.call(server)))
    237:       rescue Sequel::DatabaseDisconnectError
    238:         @conns.delete(server)
    239:         @disconnection_proc.call(c) if @disconnection_proc
    240:         raise
    241:       end
    242:     rescue Exception => e
    243:       # if the error is not a StandardError it is converted into RuntimeError.
    244:       raise(@convert_exceptions && !e.is_a?(StandardError) ? RuntimeError.new(e.message) : e)
    245:     end
    246:   end

[Validate]