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, #connect_sqls, #db

Instance Method Summary collapse

Methods inherited from ConnectionPool

#servers

Methods included from ConnectionPool::ClassMethods

#get_pool

Constructor Details

#initialize(db, opts = OPTS) ⇒ SingleConnectionPool

Returns a new instance of 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 unless @conn.empty?
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
# File 'lib/sequel/connection_pool/single.rb', line 26

def hold(server=nil)
  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

#max_sizeObject

The SingleConnectionPool always has a maximum size of 1.



37
38
39
# File 'lib/sequel/connection_pool/single.rb', line 37

def max_size
  1
end

#pool_typeObject



41
42
43
# File 'lib/sequel/connection_pool/single.rb', line 41

def pool_type
  :single
end

#sizeObject

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



47
48
49
# File 'lib/sequel/connection_pool/single.rb', line 47

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