Class Sequel::SingleThreadedPool
In: lib/sequel_core/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.

Note that using a single threaded pool with some adapters can cause errors in certain cases, see Sequel.single_threaded=.

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:

  • :pool_convert_exceptions - Whether to convert non-StandardError based exceptions to RuntimeError exceptions (default true)

[Source]

     # File lib/sequel_core/connection_pool.rb, line 222
222:   def initialize(opts={}, &block)
223:     @connection_proc = block
224:     @disconnection_proc = opts[:disconnection_proc]
225:     @conns = {}
226:     @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true
227:   end

Public Instance methods

The connection for the given server.

[Source]

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

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

[Source]

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

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

[Source]

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

[Validate]