public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/josh/rails.git
Search Repo:
 r3847@asus:  jeremy | 2006-02-26 15:26:53 -0800
 Apply [3674] to stable. Closes #3591.


git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/stable@3676 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
jeremy (author)
Sun Feb 26 15:26:47 -0800 2006
commit  ec20838381dc26b68142c84c0e03f523765b4aae
tree    ebce0fca24b7dbf50ccf38116bcf5b7740add7d0
parent  fba571a50a899d1b650905d1e158a78b287f94ef
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* ActiveRecord::Base.remove_connection explicitly closes database connections and doesn't corrupt the connection cache. Introducing the disconnect! instance method for the PostgreSQL, MySQL, and SQL Server adapters; implementations for the others are welcome. #3591 [Simon Stapleton, Tom Ward]
0
+
0
 * PostgreSQL: correctly parse negative integer column defaults. #3776 [bellis@deepthought.org]
0
 
0
 * Show a meaningful error when the DB2 adapter cannot be loaded due to missing dependencies. [Nicholas Seckar]
...
129
130
131
132
133
134
135
136
137
 
 
 
 
 
 
 
138
139
140
...
129
130
131
 
 
 
 
 
 
132
133
134
135
136
137
138
139
140
141
0
@@ -129,12 +129,13 @@ module ActiveRecord
0
     # can be used as argument for establish_connection, for easy
0
     # re-establishing of the connection.
0
     def self.remove_connection(klass=self)
0
- conn = @@defined_connections[klass.name]
0
- @@defined_connections.delete(klass.name)
0
- @@connection_cache[Thread.current.object_id].delete(klass.name)
0
- active_connections.delete(klass.name)
0
- @connection = nil
0
- conn.config if conn
0
+ spec = @@defined_connections[klass.name]
0
+ konn = active_connections[klass.name]
0
+ @@defined_connections.delete_if { |key, value| value == spec }
0
+ @@connection_cache[Thread.current.object_id].delete_if { |key, value| value == konn }
0
+ active_connections.delete_if { |key, value| value == konn }
0
+ konn.disconnect! if konn
0
+ spec.config if spec
0
     end
0
 
0
     # Set the connection for the class.
...
57
58
59
60
 
61
62
63
64
 
 
 
 
 
 
65
66
67
...
57
58
59
 
60
61
62
63
64
65
66
67
68
69
70
71
72
73
0
@@ -57,11 +57,17 @@ module ActiveRecord
0
 
0
       # Is this connection active and ready to perform queries?
0
       def active?
0
- true
0
+ @active != false
0
       end
0
 
0
       # Close this connection and open a new one in its place.
0
       def reconnect!
0
+ @active = true
0
+ end
0
+
0
+ # Close this connection
0
+ def disconnect!
0
+ @active = false
0
       end
0
 
0
 
...
160
161
162
163
 
164
165
 
 
 
 
166
167
168
...
160
161
162
 
163
164
165
166
167
168
169
170
171
172
0
@@ -160,9 +160,13 @@ module ActiveRecord
0
       end
0
 
0
       def reconnect!
0
- @connection.close rescue nil
0
+ disconnect!
0
         connect
0
       end
0
+
0
+ def disconnect!
0
+ @connection.close rescue nil
0
+ end
0
 
0
 
0
       # DATABASE STATEMENTS ======================================
...
75
76
77
 
 
 
 
 
78
79
80
...
75
76
77
78
79
80
81
82
83
84
85
0
@@ -75,6 +75,11 @@ module ActiveRecord
0
           configure_connection
0
         end
0
       end
0
+
0
+ def disconnect!
0
+ # Both postgres and postgres-pr respond to :close
0
+ @connection.close rescue nil
0
+ end
0
 
0
       def native_database_types
0
         {
...
216
217
218
219
 
220
221
222
223
224
 
 
 
 
 
 
225
226
227
...
216
217
218
 
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
0
@@ -216,12 +216,18 @@ module ActiveRecord
0
 
0
       # Reconnects to the database, returns false if no connection could be made.
0
       def reconnect!
0
- @connection.disconnect rescue nil
0
+ disconnect!
0
         @connection = DBI.connect(*@connection_options)
0
       rescue DBI::DatabaseError => e
0
         @logger.warn "#{adapter_name} reconnection failed: #{e.message}" if @logger
0
         false
0
       end
0
+
0
+ # Disconnects from the database
0
+
0
+ def disconnect!
0
+ @connection.disconnect rescue nil
0
+ end
0
 
0
       def select_all(sql, name = nil)
0
         select(sql, name)
...
7
8
9
10
 
 
11
12
13
14
 
 
15
16
17
 
18
19
 
20
 
21
22
 
23
24
 
 
 
 
25
...
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
0
@@ -7,19 +7,26 @@ class TestUnconnectedAdaptor < Test::Unit::TestCase
0
   self.use_transactional_fixtures = false
0
 
0
   def setup
0
- @connection = ActiveRecord::Base.remove_connection
0
+ @underlying = ActiveRecord::Base.connection
0
+ @specification = ActiveRecord::Base.remove_connection
0
   end
0
 
0
   def teardown
0
- ActiveRecord::Base.establish_connection(@connection)
0
+ @underlying = nil
0
+ ActiveRecord::Base.establish_connection(@specification)
0
   end
0
 
0
- def test_unconnected
0
+ def test_connection_no_longer_established
0
     assert_raise(ActiveRecord::ConnectionNotEstablished) do
0
- TestRecord.find(1)
0
+ TestRecord.find(1)
0
     end
0
+
0
     assert_raise(ActiveRecord::ConnectionNotEstablished) do
0
- TestRecord.new.save
0
+ TestRecord.new.save
0
     end
0
   end
0
+
0
+ def test_underlying_adapter_no_longer_active
0
+ assert !@underlying.active?, "Removed adapter should no longer be active"
0
+ end
0
 end

Comments

    No one has commented yet.