Skip to content
This repository
Browse code

make active_connection? return true only if there is an open connecti…

…on in use for the current thread. fixes #5330
  • Loading branch information...
commit cff19cf545de61ada8904d9d3daaaa594a9a931f 1 parent 263d842
Aaron Patterson authored March 08, 2012
7  activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -95,10 +95,11 @@ def connection
95 95
         @reserved_connections[current_connection_id] ||= checkout
96 96
       end
97 97
 
98  
-      # Check to see if there is an active connection in this connection
99  
-      # pool.
  98
+      # Is there an open connection that is being used for the current thread?
100 99
       def active_connection?
101  
-        active_connections.any?
  100
+        @reserved_connections.fetch(current_connection_id) {
  101
+          return false
  102
+        }.in_use?
102 103
       end
103 104
 
104 105
       # Signal that the thread is finished with the current connection.
42  activerecord/test/cases/connection_pool_test.rb
@@ -3,7 +3,11 @@
3 3
 module ActiveRecord
4 4
   module ConnectionAdapters
5 5
     class ConnectionPoolTest < ActiveRecord::TestCase
  6
+      attr_reader :pool
  7
+
6 8
       def setup
  9
+        super
  10
+
7 11
         # Keep a duplicate pool so we do not bother others
8 12
         @pool = ConnectionPool.new ActiveRecord::Base.connection_pool.spec
9 13
 
@@ -18,6 +22,44 @@ def setup
18 22
         end
19 23
       end
20 24
 
  25
+      def teardown
  26
+        super
  27
+        @pool.disconnect!
  28
+      end
  29
+
  30
+      def active_connections(pool)
  31
+        pool.connections.find_all(&:in_use?)
  32
+      end
  33
+
  34
+      def test_with_connection
  35
+        assert_equal 0, active_connections(pool).size
  36
+
  37
+        main_thread = pool.connection
  38
+        assert_equal 1, active_connections(pool).size
  39
+
  40
+        Thread.new {
  41
+          pool.with_connection do |conn|
  42
+            assert conn
  43
+            assert_equal 2, active_connections(pool).size
  44
+          end
  45
+          assert_equal 1, active_connections(pool).size
  46
+        }.join
  47
+
  48
+        main_thread.close
  49
+        assert_equal 0, active_connections(pool).size
  50
+      end
  51
+
  52
+      def test_active_connection_in_use
  53
+        assert !pool.active_connection?
  54
+        main_thread = pool.connection
  55
+
  56
+        assert pool.active_connection?
  57
+
  58
+        main_thread.close
  59
+
  60
+        assert !pool.active_connection?
  61
+      end
  62
+
21 63
       def test_active_connection?
22 64
         assert !@pool.active_connection?
23 65
         assert @pool.connection

3 notes on commit cff19cf

Brian Lopez

:+1:

curious if this will help any regarding brianmario/mysql2#66, brianmario/mysql2#209 or brianmario/mysql2#213

Aaron Patterson
Owner

@brianmario I don't think so... I've seen those errors before, but I can't remember exactly the problem. IIRC, it happens if you have a low traffic app where the connection timeout is not long enough, but I can't remember.

@jeremy do you recall issues like these? I seem to remember there was a fix in rails.

Xavier Noria
Owner
fxn commented on cff19cf March 09, 2012

I know of an application that reports lost connections often. It is a busy application (about 40K rpm). A priori seems strange because the connection pool does a mysql_ping on checkout, so you got a successful ping and just milliseconds later the connection is lost (or the server gone, also happens). The MySQL server on the other hand seems to be doing fine.

Not saying it is related to this patch, just a followup.

Please sign in to comment.
Something went wrong with that request. Please try again.