Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit ec20838381dc26b68142c84c0e03f523765b4aae 1 parent fba571a
@jeremy jeremy authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* 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]
+
* PostgreSQL: correctly parse negative integer column defaults. #3776 [bellis@deepthought.org]
* Show a meaningful error when the DB2 adapter cannot be loaded due to missing dependencies. [Nicholas Seckar]
View
13 activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
@@ -129,12 +129,13 @@ def self.connected?
# can be used as argument for establish_connection, for easy
# re-establishing of the connection.
def self.remove_connection(klass=self)
- conn = @@defined_connections[klass.name]
- @@defined_connections.delete(klass.name)
- @@connection_cache[Thread.current.object_id].delete(klass.name)
- active_connections.delete(klass.name)
- @connection = nil
- conn.config if conn
+ spec = @@defined_connections[klass.name]
+ konn = active_connections[klass.name]
+ @@defined_connections.delete_if { |key, value| value == spec }
+ @@connection_cache[Thread.current.object_id].delete_if { |key, value| value == konn }
+ active_connections.delete_if { |key, value| value == konn }
+ konn.disconnect! if konn
+ spec.config if spec
end
# Set the connection for the class.
View
8 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -57,11 +57,17 @@ def reset_runtime #:nodoc:
# Is this connection active and ready to perform queries?
def active?
- true
+ @active != false
end
# Close this connection and open a new one in its place.
def reconnect!
+ @active = true
+ end
+
+ # Close this connection
+ def disconnect!
+ @active = false
end
View
6 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -160,9 +160,13 @@ def active?
end
def reconnect!
- @connection.close rescue nil
+ disconnect!
connect
end
+
+ def disconnect!
+ @connection.close rescue nil
+ end
# DATABASE STATEMENTS ======================================
View
5 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -75,6 +75,11 @@ def reconnect!
configure_connection
end
end
+
+ def disconnect!
+ # Both postgres and postgres-pr respond to :close
+ @connection.close rescue nil
+ end
def native_database_types
{
View
8 activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
@@ -216,12 +216,18 @@ def active?
# Reconnects to the database, returns false if no connection could be made.
def reconnect!
- @connection.disconnect rescue nil
+ disconnect!
@connection = DBI.connect(*@connection_options)
rescue DBI::DatabaseError => e
@logger.warn "#{adapter_name} reconnection failed: #{e.message}" if @logger
false
end
+
+ # Disconnects from the database
+
+ def disconnect!
+ @connection.disconnect rescue nil
+ end
def select_all(sql, name = nil)
select(sql, name)
View
17 activerecord/test/unconnected_test.rb
@@ -7,19 +7,26 @@ class TestUnconnectedAdaptor < Test::Unit::TestCase
self.use_transactional_fixtures = false
def setup
- @connection = ActiveRecord::Base.remove_connection
+ @underlying = ActiveRecord::Base.connection
+ @specification = ActiveRecord::Base.remove_connection
end
def teardown
- ActiveRecord::Base.establish_connection(@connection)
+ @underlying = nil
+ ActiveRecord::Base.establish_connection(@specification)
end
- def test_unconnected
+ def test_connection_no_longer_established
assert_raise(ActiveRecord::ConnectionNotEstablished) do
- TestRecord.find(1)
+ TestRecord.find(1)
end
+
assert_raise(ActiveRecord::ConnectionNotEstablished) do
- TestRecord.new.save
+ TestRecord.new.save
end
end
+
+ def test_underlying_adapter_no_longer_active
+ assert !@underlying.active?, "Removed adapter should no longer be active"
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.