Class: ActiveRecord::Bogacs::FalsePool

Inherits:
Object
  • Object
show all
Includes:
PoolSupport, ThreadSafe::Synchronized
Defined in:
lib/active_record/bogacs/false_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ThreadSafe::Synchronized

#synchronize

Methods included from PoolSupport

#current_connection_id, #new_connection

Constructor Details

#initialize(spec) ⇒ FalsePool

Returns a new instance of FalsePool.



18
19
20
21
22
23
24
25
26
# File 'lib/active_record/bogacs/false_pool.rb', line 18

def initialize(spec)
  @connected = nil

  @spec = spec
  @size = nil
  @automatic_reconnect = nil

  @reserved_connections = ThreadSafe::Map.new #:initial_capacity => @size
end

Instance Attribute Details

#automatic_reconnectObject

Returns the value of attribute automatic_reconnect.



14
15
16
# File 'lib/active_record/bogacs/false_pool.rb', line 14

def automatic_reconnect
  @automatic_reconnect
end

#sizeObject (readonly)

Returns the value of attribute size.



16
17
18
# File 'lib/active_record/bogacs/false_pool.rb', line 16

def size
  @size
end

#specObject (readonly)

Returns the value of attribute spec.



16
17
18
# File 'lib/active_record/bogacs/false_pool.rb', line 16

def spec
  @spec
end

Instance Method Details

#active_connection?Boolean

Is there an open connection that is being used for the current thread?

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/active_record/bogacs/false_pool.rb', line 47

def active_connection?
  conn = @reserved_connections[current_connection_id]
  conn ? conn.in_use? : false
end

#checkin(conn, do_release = true) ⇒ Object

Check-in a database connection back into the pool, indicating that you no longer need this connection.

object, which was obtained earlier by calling #checkout on this pool

Parameters:

  • conn (ActiveRecord::ConnectionAdapters::AbstractAdapter)

    connection

See Also:



148
149
150
151
152
153
154
155
# File 'lib/active_record/bogacs/false_pool.rb', line 148

def checkin(conn, do_release = true)
  release(conn) if do_release
  #synchronize do
    _run_checkin_callbacks(conn)
    #release conn
    #@available.add conn
  #end
end

#checkoutActiveRecord::ConnectionAdapters::AbstractAdapter

Check-out a database connection from the pool, indicating that you want to use it. You should call #checkin when you no longer need this.

Returns:

  • (ActiveRecord::ConnectionAdapters::AbstractAdapter)

Raises:

  • (ActiveRecord::ConnectionTimeoutError)

    no connection can be obtained from the pool



133
134
135
136
137
138
139
140
# File 'lib/active_record/bogacs/false_pool.rb', line 133

def checkout
  #synchronize do
    conn = checkout_new_connection # acquire_connection
    conn.lease
    _run_checkout_callbacks(conn) # checkout_and_verify(conn)
    conn
  #end
end

#checkout_timeoutObject



35
# File 'lib/active_record/bogacs/false_pool.rb', line 35

def checkout_timeout; end

#clear_reloadable_connections!Object

Clears the cache which maps classes.



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_record/bogacs/false_pool.rb', line 90

def clear_reloadable_connections!
  synchronize do
    @connected = false

    connections = @reserved_connections.values
    @reserved_connections.clear

    connections.each do |conn|
      checkin conn
      conn.disconnect! if conn.requires_reloading?
    end
  end
end

#clear_stale_cached_connections!Object

Return any checked-out connections back to the pool by threads that are no longer alive.



117
118
119
120
121
122
123
124
125
126
# File 'lib/active_record/bogacs/false_pool.rb', line 117

def clear_stale_cached_connections!
  keys = Thread.list.find_all { |t| t.alive? }.map(&:object_id)
  keys = @reserved_connections.keys - keys
  keys.each do |key|
    if conn = @reserved_connections[key]
      checkin conn, false # no release
      @reserved_connections.delete(key)
    end
  end
end

#connected?Boolean

Returns true if a connection has already been opened.

Returns:

  • (Boolean)


72
# File 'lib/active_record/bogacs/false_pool.rb', line 72

def connected?; @connected end

#connectionObject

Retrieve the connection associated with the current thread, or call #checkout to obtain one if necessary.

#connection can be called any number of times; the connection is held in a hash keyed by the thread id.



42
43
44
# File 'lib/active_record/bogacs/false_pool.rb', line 42

def connection
  @reserved_connections[current_connection_id] ||= checkout
end

#connectionsObject



29
# File 'lib/active_record/bogacs/false_pool.rb', line 29

def connections; @reserved_connections.values end

#disconnect!Object

Disconnects all connections in the pool, and clears the pool.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_record/bogacs/false_pool.rb', line 75

def disconnect!
  synchronize do
    @connected = false

    connections = @reserved_connections.values
    @reserved_connections.clear

    connections.each do |conn|
      checkin conn
      conn.disconnect!
    end
  end
end

#reapObject



164
165
166
# File 'lib/active_record/bogacs/false_pool.rb', line 164

def reap
  # we do not really manage the connection pool - nothing to do ...
end

#reaperObject



32
# File 'lib/active_record/bogacs/false_pool.rb', line 32

def reaper; end

#release_connection(with_id = current_connection_id) ⇒ Object

Signal that the thread is finished with the current connection. #release_connection releases the connection-thread association and returns the connection to the pool.



55
56
57
58
# File 'lib/active_record/bogacs/false_pool.rb', line 55

def release_connection(with_id = current_connection_id)
  conn = @reserved_connections.delete(with_id)
  checkin conn if conn
end

#remove(conn) ⇒ Object

Remove a connection from the connection pool. The connection will remain open and active but will no longer be managed by this pool.



159
160
161
# File 'lib/active_record/bogacs/false_pool.rb', line 159

def remove(conn)
  release(conn)
end

#verify_active_connections!Object

Verify active connections and remove and disconnect connections associated with stale threads.



107
108
109
110
111
112
# File 'lib/active_record/bogacs/false_pool.rb', line 107

def verify_active_connections!
  synchronize do
    clear_stale_cached_connections!
    @reserved_connections.values.each(&:verify!)
  end
end

#with_connectionObject

If a connection already exists yield it to the block. If no connection exists checkout a connection, yield it to the block, and checkin the connection when finished.



63
64
65
66
67
68
69
# File 'lib/active_record/bogacs/false_pool.rb', line 63

def with_connection
  connection_id = current_connection_id
  fresh_connection = true unless active_connection?
  yield connection
ensure
  release_connection(connection_id) if fresh_connection
end