Class: ActiveRecord::ConnectionAdapters::MakaraAbstractAdapter::ActiveRecordPoolControl

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/makara_abstract_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy) ⇒ ActiveRecordPoolControl

Returns a new instance of ActiveRecordPoolControl.



190
191
192
193
194
195
196
197
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 190

def initialize(proxy)
  @proxy = proxy
  @owner = nil
  @pool = nil
  @schema_cache = ActiveRecord::ConnectionAdapters::SchemaCache.new @proxy
  @idle_since = Concurrent.monotonic_time
  @adapter = ActiveRecord::ConnectionAdapters::AbstractAdapter.new(@proxy)
end

Instance Attribute Details

#ownerObject (readonly) Also known as: in_use?

Returns the value of attribute owner.



187
188
189
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 187

def owner
  @owner
end

Instance Method Details

#==(*args) ⇒ Object



283
284
285
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 283

def ==(*args)
  @proxy.object_id == args[0].object_id
end

#close(*args) ⇒ Object



199
200
201
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 199

def close(*args)
  @pool.checkin @proxy
end

#expire(*args) ⇒ Object

this method must only be called while holding connection pool’s mutex



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 219

def expire(*args)
  if in_use?
    if @owner != Thread.current
      raise ActiveRecordError, "Cannot expire connection, " \
        "it is owned by a different thread: #{@owner}. " \
        "Current thread: #{Thread.current}."
    end

    @idle_since = Concurrent.monotonic_time
    @owner = nil
  else
    raise ActiveRecordError, "Cannot expire connection, it is not currently leased."
  end
end

#lease(*args) ⇒ Object

this method must only be called while holding connection pool’s mutex



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 204

def lease(*args)
  if in_use?
    msg = +"Cannot lease connection, "
    if @owner == Thread.current
      msg << "it is already leased by the current thread."
    else
      msg << "it is already in use by a different thread: #{@owner}. " \
            "Current thread: #{Thread.current}."
    end
    raise ActiveRecordError, msg
  end
  @owner = Thread.current
end

#lock(*args) ⇒ Object



271
272
273
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 271

def lock(*args)
  @adapter.lock
end

#pool(*args) ⇒ Object



279
280
281
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 279

def pool(*args)
  @pool
end

#pool=(*args) ⇒ Object



275
276
277
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 275

def pool=(*args)
  @pool = args[0]
end

#schema_cache(*args) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 253

def schema_cache(*args)
  if @pool.respond_to?(:get_schema_cache) # AR6
    @pool.get_schema_cache(@proxy)
  else
    @schema_cache
  end
end

#schema_cache=(*args) ⇒ Object



261
262
263
264
265
266
267
268
269
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 261

def schema_cache=(*args)
  cache = args[0]
  cache.connection = @proxy
  if @pool.respond_to?(:set_schema_cache) # AR6
    @pool.set_schema_cache(cache)
  else
    @schema_cache = cache
  end
end

#seconds_idle(*args) ⇒ Object

Seconds since this connection was returned to the pool



235
236
237
238
239
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 235

def seconds_idle(*args)
  return 0 if in_use?

  Concurrent.monotonic_time - @idle_since
end

#steal!(*args) ⇒ Object

this method must only be called while holding connection pool’s mutex (and a desire for segfaults)



242
243
244
245
246
247
248
249
250
251
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 242

def steal!(*args)
  if in_use?
    if @owner != Thread.current
      @pool.send :remove_connection_from_thread_cache, @proxy, @owner
      @owner = Thread.current
    end
  else
    raise ActiveRecordError, "Cannot steal connection, it is not currently leased."
  end
end