Class: Sequel::SingleConnectionPool

Inherits:
ConnectionPool show all
Defined in:
lib/sequel/connection_pool/single.rb

Overview

This is the fastest connection pool, since it isn't a connection pool at all. It is just a wrapper around a single connection that uses the connection pool API.

Constant Summary

Constants inherited from ConnectionPool

ConnectionPool::OPTS, ConnectionPool::POOL_CLASS_MAP

Instance Attribute Summary

Attributes inherited from ConnectionPool

#after_connect, #db

Instance Method Summary collapse

Methods inherited from ConnectionPool

#servers

Methods included from ConnectionPool::ClassMethods

#get_pool

Constructor Details

#initialize(db, opts = OPTS) ⇒ SingleConnectionPool



7
8
9
10
# File 'lib/sequel/connection_pool/single.rb', line 7

def initialize(db, opts=OPTS)
  super
  @conn = []
end

Instance Method Details

#all_connections {|@conn.first| ... } ⇒ Object

Yield the connection if one has been made.

Yields:

  • (@conn.first)


13
14
15
# File 'lib/sequel/connection_pool/single.rb', line 13

def all_connections
  yield @conn.first if @conn
end

#disconnect(opts = nil) ⇒ Object

Disconnect the connection from the database.



18
19
20
21
22
23
# File 'lib/sequel/connection_pool/single.rb', line 18

def disconnect(opts=nil)
  return unless c = @conn.first
  disconnect_connection(c)
  @conn.clear
  nil
end

#hold(server = nil) ⇒ Object

Yield the connection to the block.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sequel/connection_pool/single.rb', line 26

def hold(server=nil)
  begin
    unless c = @conn.first
      @conn.replace([c = make_new(:default)])
    end
    yield c
  rescue Sequel::DatabaseDisconnectError, *@error_classes => e
    disconnect if disconnect_error?(e)
    raise
  end
end

#max_sizeObject

The SingleConnectionPool always has a maximum size of 1.



39
40
41
# File 'lib/sequel/connection_pool/single.rb', line 39

def max_size
  1
end

#pool_typeObject



43
44
45
# File 'lib/sequel/connection_pool/single.rb', line 43

def pool_type
  :single
end

#sizeObject

The SingleConnectionPool always has a size of 1 if connected and 0 if not.



49
50
51
# File 'lib/sequel/connection_pool/single.rb', line 49

def size
  @conn.empty? ? 0 : 1
end