github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

coderrr / cleanup_connection

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 6
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

monkey patch to make ActiveRecord connection cleanup automatic — Read more

  cancel

http://coderrr.wordpress.com/2009/01/16/monkey-patching-activerecord-to-automatically-release-connections/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Added AssociationCollection#count to wrapped methods 
coderrr (author)
Tue May 05 15:21:46 -0700 2009
commit  d2acb1db4c56dbe15aa72a6ae67f33859836b097
tree    f7fbdde802a702680ac0ce9571546ddd8c6cd69e
parent  1bd591cbcf09be20dab626140ddf98a10e28731f
cleanup_connection / cleanup_connection_patch.rb cleanup_connection_patch.rb
100644 67 lines (61 sloc) 2.388 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module ActiveRecord
  module ConnectionAdapters
    class ConnectionPool
      def cleanup_connection
        return yield if Thread.current[:__AR__cleanup_connection]
 
        begin
          Thread.current[:__AR__cleanup_connection] = true
          yield
        ensure
          release_connection
          Thread.current[:__AR__cleanup_connection] = false
        end
      end
    end
  end
 
  class Base
    class << self
      def cleanup_connection(&block)
        connection_pool.cleanup_connection(&block)
      end
 
      # comment out this redefinition once you've wrapped all necessary methods
      alias_method :connection_without_cleanup_connection_check, :connection
      def connection(*a)
        if ! Thread.current[:__AR__cleanup_connection]
          puts "connection called outside of cleanup_connection block", caller, "\n"
        end
        connection_without_cleanup_connection_check(*a)
      end
    end
  end
end
 
methods_to_wrap = {
  (class<<ActiveRecord::Base;self;end) => [
    :find, :find_every, :find_by_sql, :transaction, :count, :create, :delete, :count_by_sql,
    :update, :destroy, :cache, :uncached, :quoted_table_name, :columns, :exists?, :update_all,
    :increment_counter, :decrement_counter, :delete_all, :table_exists?, :update_counters,
  ],
  ActiveRecord::Base => [:quoted_id, :valid?],
  ActiveRecord::Associations::AssociationCollection => [:initialize, :find, :find_target, :load_target, :count],
  ActiveRecord::Associations::HasAndBelongsToManyAssociation => [:create],
  ActiveRecord::Associations::HasManyThroughAssociation => [:construct_conditions],
  ActiveRecord::Associations::HasOneAssociation => [:construct_sql],
  ActiveRecord::Associations::ClassMethods => [:collection_reader_method, :configure_dependency_for_has_many],
  ActiveRecord::Calculations::ClassMethods => [:calculate],
}
methods_to_wrap[Test::Unit::TestSuite] = [:run] if defined?(Test::Unit::TestSuite)
 
methods_to_wrap.each do |klass, methods|
  methods.each do |method|
    klass.class_eval do
      alias_method_chain method, :cleanup_connection do |target, punc|
        eval %{
def #{target}_with_cleanup_connection#{punc}(*a, &b)
ActiveRecord::Base.connection_pool.cleanup_connection do
#{target}_without_cleanup_connection#{punc}(*a, &b)
end
end
}
      end
    end
  end
end
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server