Class: NeverBlock::DB::PooledDBConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/never_block/db/pooled_db_connection.rb

Overview

a proxy for pooled fibered connections

Instance Method Summary collapse

Constructor Details

#initialize(size = 4, &block) ⇒ PooledDBConnection

Requires a block with connection parameters and a pool size (defaults to 4)



7
8
9
10
11
# File 'lib/never_block/db/pooled_db_connection.rb', line 7

def initialize(size=4, &block)
  @pool = NB::Pool::FiberedConnectionPool.new(:size=>size, :eager=>true) do
    yield
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Pass unknown methods to the connection



29
30
31
32
33
# File 'lib/never_block/db/pooled_db_connection.rb', line 29

def method_missing(method, *args)
  @pool.hold do |conn|
    conn.send(method, *args)
  end        
end

Instance Method Details

#query(query) ⇒ Object Also known as: exec

A proxy for the connection’s query method quries the pool to get a connection first



15
16
17
18
19
# File 'lib/never_block/db/pooled_db_connection.rb', line 15

def query(query)
  @pool.hold do |conn|
    conn.query(query)
  end
end

#replace_acquired_connectionObject

Replaces the current connection with a brand new one



24
25
26
# File 'lib/never_block/db/pooled_db_connection.rb', line 24

def replace_acquired_connection
  @pool.replace_acquired_connection
end

#respond_to?(method) ⇒ Boolean

Pass method queries to the connection

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/never_block/db/pooled_db_connection.rb', line 36

def respond_to?(method)
  @pool.hold do |conn|
    conn.respond_to?(method)
  end        
end