Class: DataMapper::Support::ConnectionPool

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

Overview

A ConnectionPool manages access to database connections by keeping multiple connections and giving threads exclusive access to each connection.

CREDIT: Sharon Rosner, maintainer of the Sequel (sequel.rubyforge.org) project an “ORM framework for Ruby” contributed this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = 4, &block) ⇒ ConnectionPool

Constructs a new pool with a maximum size. If a block is supplied, it is used to create new connections as they are needed.

pool = ConnectionPool.new(10) {MyConnection.new(opts)}

The connection creation proc can be changed at any time by assigning a Proc to pool#connection_proc.

pool = ConnectionPool.new(10)
pool.connection_proc = proc {MyConnection.new(opts)}


33
34
35
36
37
38
39
40
41
# File 'lib/data_mapper/support/connection_pool.rb', line 33

def initialize(max_size = 4, &block)
  @max_size = max_size
  @mutex = Mutex.new
  @connection_proc = block

  @available_connections = []
  @allocated = {}
  @created_count = 0
end

Instance Attribute Details

#allocatedObject (readonly)

Returns the value of attribute allocated.



21
22
23
# File 'lib/data_mapper/support/connection_pool.rb', line 21

def allocated
  @allocated
end

#available_connectionsObject (readonly)

Returns the value of attribute available_connections.



21
22
23
# File 'lib/data_mapper/support/connection_pool.rb', line 21

def available_connections
  @available_connections
end

#connection_procObject

The proc used to create a new connection.



19
20
21
# File 'lib/data_mapper/support/connection_pool.rb', line 19

def connection_proc
  @connection_proc
end

#created_countObject (readonly)

Returns the value of attribute created_count.



21
22
23
# File 'lib/data_mapper/support/connection_pool.rb', line 21

def created_count
  @created_count
end

#max_sizeObject (readonly)

The maximum number of connections.



16
17
18
# File 'lib/data_mapper/support/connection_pool.rb', line 16

def max_size
  @max_size
end

#mutexObject (readonly)

Returns the value of attribute mutex.



13
14
15
# File 'lib/data_mapper/support/connection_pool.rb', line 13

def mutex
  @mutex
end

Instance Method Details

#holdObject

Assigns a connection to the current thread, yielding the connection to the supplied block.

pool.hold {|conn| conn.execute('DROP TABLE posts;')}

Pool#hold is re-entrant, meaning it can be called recursively in the same thread without blocking.

If no connection is available, Pool#hold will block until a connection is available.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/data_mapper/support/connection_pool.rb', line 58

def hold
  t = Thread.current
  if (conn = owned_connection(t))
    return yield(conn)
  end
  while !(conn = acquire(t))
    sleep 0.001
  end
  begin
    yield conn
  ensure
    release(t)
  end
rescue Exception => e
  # if the error is not a StandardError it is converted into RuntimeError.
  raise e.is_a?(StandardError) ? e : e.message
end

#sizeObject

Returns the number of created connections.



44
45
46
# File 'lib/data_mapper/support/connection_pool.rb', line 44

def size
  @created_count
end