Skip to content

Commit

Permalink
Adapters keep in_use flag when leased
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Nov 29, 2011
1 parent 7837574 commit 9bf8bf8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Expand Up @@ -4,6 +4,7 @@
require 'active_support/core_ext/benchmark' require 'active_support/core_ext/benchmark'
require 'active_support/deprecation' require 'active_support/deprecation'
require 'active_record/connection_adapters/schema_cache' require 'active_record/connection_adapters/schema_cache'
require 'monitor'


module ActiveRecord module ActiveRecord
module ConnectionAdapters # :nodoc: module ConnectionAdapters # :nodoc:
Expand Down Expand Up @@ -48,13 +49,17 @@ class AbstractAdapter
include DatabaseLimits include DatabaseLimits
include QueryCache include QueryCache
include ActiveSupport::Callbacks include ActiveSupport::Callbacks
include MonitorMixin


define_callbacks :checkout, :checkin define_callbacks :checkout, :checkin


attr_accessor :visitor attr_accessor :visitor
attr_reader :schema_cache attr_reader :schema_cache, :last_use, :in_use
alias :in_use? :in_use


def initialize(connection, logger = nil) #:nodoc: def initialize(connection, logger = nil) #:nodoc:
super()

@active = nil @active = nil
@connection, @logger = connection, logger @connection, @logger = connection, logger
@query_cache_enabled = false @query_cache_enabled = false
Expand All @@ -63,6 +68,14 @@ def initialize(connection, logger = nil) #:nodoc:
@instrumenter = ActiveSupport::Notifications.instrumenter @instrumenter = ActiveSupport::Notifications.instrumenter
@visitor = nil @visitor = nil
@schema_cache = SchemaCache.new self @schema_cache = SchemaCache.new self
@in_use = false
end

def lease
synchronize do
@in_use = true
@last_use = Time.now
end
end end


# Returns the human-readable name of the adapter. Use mixed case - one # Returns the human-readable name of the adapter. Use mixed case - one
Expand Down
@@ -0,0 +1,16 @@
require "cases/helper"

module ActiveRecord
module ConnectionAdapters
class AbstractAdapterTest < ActiveRecord::TestCase
def test_in_use?
adapter = AbstractAdapter.new nil, nil

# FIXME: change to refute in Rails 4.0 / mt
assert !adapter.in_use?, 'adapter is not in use'
assert adapter.lease, 'lease adapter'
assert adapter.in_use?, 'adapter is in use'
end
end
end
end

0 comments on commit 9bf8bf8

Please sign in to comment.