Class: Sequel::SingleThreadedPool

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_core/connection_pool.rb

Overview

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=.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ SingleThreadedPool

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)



171
172
173
174
# File 'lib/sequel_core/connection_pool.rb', line 171

def initialize(opts={}, &block)
  @connection_proc = block
  @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true
end

Instance Attribute Details

#connObject (readonly)

The single database connection for the pool



160
161
162
# File 'lib/sequel_core/connection_pool.rb', line 160

def conn
  @conn
end

#connection_proc=(value) ⇒ Object (writeonly)

The proc used to create a new database connection



163
164
165
# File 'lib/sequel_core/connection_pool.rb', line 163

def connection_proc=(value)
  @connection_proc = value
end

Instance Method Details

#disconnect(&block) ⇒ Object

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



190
191
192
193
# File 'lib/sequel_core/connection_pool.rb', line 190

def disconnect(&block)
  block[@conn] if block && @conn
  @conn = nil
end

#holdObject

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



178
179
180
181
182
183
184
185
186
# File 'lib/sequel_core/connection_pool.rb', line 178

def hold
  begin
    @conn ||= @connection_proc.call
    yield @conn
  rescue Exception => e
    # if the error is not a StandardError it is converted into RuntimeError.
    raise(@convert_exceptions && !e.is_a?(StandardError) ? RuntimeError.new(e.message) : e)
  end
end