Skip to content
This repository
Browse code

Automatic closure of connections in threads is deprecated. For example

the following code is deprecated:

Thread.new { Post.find(1) }.join

It should be changed to close the database connection at the end of
the thread:

Thread.new {
  Post.find(1)
  Post.connection.close
}.join

Only people who spawn threads in their application code need to worry
about this change.
  • Loading branch information...
commit 0e2477b602b3aa5b66c849d19737a8b66c73f633 1 parent 29d2040
Aaron Patterson authored November 29, 2011
16  activerecord/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,21 @@
1 1
 ## Rails 3.2.0 (unreleased) ##
2 2
 
  3
+*   Automatic closure of connections in threads is deprecated.  For example
  4
+    the following code is deprecated:
  5
+
  6
+    Thread.new { Post.find(1) }.join
  7
+
  8
+    It should be changed to close the database connection at the end of
  9
+    the thread:
  10
+
  11
+    Thread.new {
  12
+      Post.find(1)
  13
+      Post.connection.close
  14
+    }.join
  15
+    
  16
+    Only people who spawn threads in their application code need to worry
  17
+    about this change.
  18
+
3 19
 *   Deprecated:
4 20
 
5 21
       * `set_table_name`
8  activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -190,7 +190,13 @@ def clear_stale_cached_connections!
190 190
           t.alive?
191 191
         }.map { |thread| thread.object_id }
192 192
         keys.each do |key|
193  
-          checkin @reserved_connections[key]
  193
+          conn = @reserved_connections[key]
  194
+          ActiveSupport::Deprecation.warn(<<-eowarn) if conn.in_use?
  195
+Database connections will not be closed automatically, please close your
  196
+database connection at the end of the thread by calling `close` on your
  197
+connection.  For example: ActiveRecord::Base.connection.close
  198
+          eowarn
  199
+          checkin conn
194 200
           @reserved_connections.delete(key)
195 201
         end
196 202
       end
31  activerecord/test/cases/connection_pool_test.rb
@@ -26,30 +26,6 @@ def test_active_connection?
26 26
         assert !@pool.active_connection?
27 27
       end
28 28
 
29  
-      def test_clear_stale_cached_connections!
30  
-        pool = ConnectionPool.new ActiveRecord::Base.connection_pool.spec
31  
-
32  
-        threads = [
33  
-          Thread.new { pool.connection },
34  
-          Thread.new { pool.connection }]
35  
-
36  
-        threads.map { |t| t.join }
37  
-
38  
-        pool.extend Module.new {
39  
-          attr_accessor :checkins
40  
-          def checkin conn
41  
-            @checkins << conn
42  
-            conn.object_id
43  
-          end
44  
-        }
45  
-        pool.checkins = []
46  
-
47  
-        cleared_threads = pool.clear_stale_cached_connections!
48  
-        assert((cleared_threads - threads.map { |x| x.object_id }).empty?,
49  
-               "threads should have been removed")
50  
-        assert_equal pool.checkins.length, threads.length
51  
-      end
52  
-
53 29
       def test_checkout_behaviour
54 30
         pool = ConnectionPool.new ActiveRecord::Base.connection_pool.spec
55 31
         connection = pool.connection
@@ -70,12 +46,15 @@ def test_checkout_behaviour
70 46
             assert thread_ids.include?(t.object_id)
71 47
           end
72 48
 
73  
-          pool.connection
  49
+          assert_deprecated do
  50
+            pool.connection
  51
+          end
74 52
           threads.each do |t|
75 53
             thread_ids = pool.instance_variable_get(:@reserved_connections).keys
76 54
             assert !thread_ids.include?(t.object_id)
77 55
           end
78  
-        end.join()
  56
+          pool.connection.close
  57
+        end.join
79 58
 
80 59
       end
81 60
 
3  activerecord/test/cases/transactions_test.rb
@@ -563,6 +563,7 @@ def test_transaction_per_thread
563 563
               topic.approved = !topic.approved?
564 564
               topic.save!
565 565
             end
  566
+            Topic.connection.close
566 567
           end
567 568
         end
568 569
 
@@ -598,6 +599,7 @@ def test_transaction_isolation__read_committed
598 599
               dev = Developer.find(1)
599 600
               assert_equal original_salary, dev.salary
600 601
             end
  602
+            Developer.connection.close
601 603
           end
602 604
         end
603 605
 
@@ -610,6 +612,7 @@ def test_transaction_isolation__read_committed
610 612
               assert_equal original_salary, Developer.find(1).salary
611 613
             end
612 614
           end
  615
+          Developer.connection.close
613 616
         end
614 617
 
615 618
         threads.each { |t| t.join }

0 notes on commit 0e2477b

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