Class: Rooq::PooledConnectionProvider

Inherits:
ConnectionProvider show all
Defined in:
lib/rooq/connection.rb

Overview

A connection provider that acquires connections from a pool. Connections are returned to the pool after each query.

The pool must respond to #checkout (or #acquire) and optionally #checkin. If the pool doesn't respond to #checkin, the connection's #close method is called.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConnectionProvider

#with_connection

Constructor Details

#initialize(pool) ⇒ PooledConnectionProvider

Returns a new instance of PooledConnectionProvider.

Parameters:



73
74
75
76
# File 'lib/rooq/connection.rb', line 73

def initialize(pool)
  super()
  @pool = pool
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



70
71
72
# File 'lib/rooq/connection.rb', line 70

def pool
  @pool
end

Instance Method Details

#acquireObject

Acquire a connection from the pool.

Returns:

  • (Object)

    a database connection



80
81
82
83
84
85
86
87
88
# File 'lib/rooq/connection.rb', line 80

def acquire
  if @pool.respond_to?(:checkout)
    @pool.checkout
  elsif @pool.respond_to?(:acquire)
    @pool.acquire
  else
    raise Error, "Pool must respond to #checkout or #acquire"
  end
end

#release(connection) ⇒ Object

Release a connection back to the pool.

Parameters:

  • connection (Object)

    the connection to release



92
93
94
95
96
97
98
# File 'lib/rooq/connection.rb', line 92

def release(connection)
  if @pool.respond_to?(:checkin)
    @pool.checkin(connection)
  elsif connection.respond_to?(:close)
    connection.close
  end
end