From 57bc25c5f8129f57b08a2dc7c319b86778dd8a40 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Sun, 9 Jan 2011 10:15:05 -0800 Subject: Use run_callbacks; the generated _run__callbacks method is not a public interface. Signed-off-by: Santiago Pastorino --- .../lib/active_record/connection_adapters/abstract/connection_pool.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index cffa2387de..54f70c59f8 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -212,7 +212,7 @@ module ActiveRecord # calling +checkout+ on this pool. def checkin(conn) @connection_mutex.synchronize do - conn.send(:_run_checkin_callbacks) do + conn.run_callbacks :checkin do @checked_out.delete conn @queue.signal end -- cgit v1.2.3 From 5f3cf4244de4fd62049d08df4e6bf63c945ab90e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 4 Feb 2011 10:19:02 -0800 Subject: connection pool can cache column, table, and primary key information --- .../abstract/connection_pool.rb | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index 54f70c59f8..bafd79d614 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -58,6 +58,7 @@ module ActiveRecord # before giving up and raising a timeout error (default 5 seconds). class ConnectionPool attr_reader :spec, :connections + attr_reader :columns, :columns_hash, :primary_keys # Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification # object which describes database connection information (e.g. adapter, @@ -81,6 +82,39 @@ module ActiveRecord @connections = [] @checked_out = [] + + @columns = Hash.new do |h, table_name| + h[table_name] = with_connection do |conn| + conn.columns(table_name, "#{table_name} Columns") + end + end + + @columns_hash = Hash.new do |h, table_name| + h[table_name] = Hash[columns[table_name].map { |col| + [col.name, col] + }] + end + + @primary_keys = Hash.new do |h, table_name| + h[table_name] = with_connection do |conn| + if conn.table_exists?(table_name) + conn.primary_key(table_name) + else + 'id' + end + end + end + end + + # Clears out internal caches: + # + # * columns + # * columns_hash + # * primary_keys + def clear_cache! + @columns.clear + @columns_hash.clear + @primary_keys.clear end # Retrieve the connection associated with the current thread, or call -- cgit v1.2.3 From 0cd42864e3dcba520980f952be9a09c01beddb03 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 4 Feb 2011 12:15:30 -0800 Subject: making sure primary key is set on the columns --- .../connection_adapters/abstract/connection_pool.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index bafd79d614..db745e325f 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -85,7 +85,15 @@ module ActiveRecord @columns = Hash.new do |h, table_name| h[table_name] = with_connection do |conn| - conn.columns(table_name, "#{table_name} Columns") + + # Fetch a list of columns + conn.columns(table_name, "#{table_name} Columns").tap do |columns| + + # set primary key information + columns.each do |column| + column.primary = column.name == primary_keys[table_name] + end + end end end -- cgit v1.2.3 From c94651f8c822f7c0778c03eb36bee5ca19f35911 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 4 Feb 2011 14:52:05 -0800 Subject: almost fisted --- .../active_record/connection_adapters/abstract/connection_pool.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index db745e325f..63cdce5e4d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -125,6 +125,13 @@ module ActiveRecord @primary_keys.clear end + # Clear out internal caches for table with +table_name+ + def clear_table_cache!(table_name) + @columns.delete table_name + @columns_hash.delete table_name + @primary_keys.delete table_name + end + # Retrieve the connection associated with the current thread, or call # #checkout to obtain one if necessary. # -- cgit v1.2.3 From acccb72cb12ab55bb01c3dce32f54f4a59ebec6c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 4 Feb 2011 15:54:32 -0800 Subject: column cache now lives on the connection pool --- .../active_record/connection_adapters/abstract/connection_pool.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index 63cdce5e4d..b9bfad2bd1 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -57,6 +57,7 @@ module ActiveRecord # * +wait_timeout+: number of seconds to block and wait for a connection # before giving up and raising a timeout error (default 5 seconds). class ConnectionPool + attr_accessor :automatic_reconnect attr_reader :spec, :connections attr_reader :columns, :columns_hash, :primary_keys @@ -82,6 +83,7 @@ module ActiveRecord @connections = [] @checked_out = [] + @automatic_reconnect = true @columns = Hash.new do |h, table_name| h[table_name] = with_connection do |conn| @@ -281,6 +283,8 @@ module ActiveRecord end def checkout_new_connection + raise ConnectionNotEstablished unless @automatic_reconnect + c = new_connection @connections << c checkout_and_verify(c) @@ -379,7 +383,7 @@ module ActiveRecord pool = @connection_pools[klass.name] return nil unless pool - @connection_pools.delete_if { |key, value| value == pool } + pool.automatic_reconnect = false pool.disconnect! pool.spec.config end -- cgit v1.2.3 From 59f7780a3454a14054d1d33d9b6e31192ab2e58b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 4 Feb 2011 18:08:31 -0800 Subject: adjust query counts to be consistent across databases, make sure database log the same things --- .../connection_adapters/abstract/connection_pool.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index b9bfad2bd1..4475019e0e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -85,6 +85,12 @@ module ActiveRecord @checked_out = [] @automatic_reconnect = true + @tables = Hash.new do |h, table_name| + with_connection do |conn| + h[table_name] = conn.table_exists?(table_name) + end + end + @columns = Hash.new do |h, table_name| h[table_name] = with_connection do |conn| @@ -107,11 +113,7 @@ module ActiveRecord @primary_keys = Hash.new do |h, table_name| h[table_name] = with_connection do |conn| - if conn.table_exists?(table_name) - conn.primary_key(table_name) - else - 'id' - end + @tables[table_name] ? conn.primary_key(table_name) : 'id' end end end @@ -121,10 +123,12 @@ module ActiveRecord # * columns # * columns_hash # * primary_keys + # * tables def clear_cache! @columns.clear @columns_hash.clear @primary_keys.clear + @tables.clear end # Clear out internal caches for table with +table_name+ -- cgit v1.2.3 From 0de661d6c74172a9fedcced6a4e99d007df953ef Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 7 Feb 2011 09:25:57 -0800 Subject: the connection pool caches table_exists? calls --- .../abstract/connection_pool.rb | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index 4475019e0e..b1754e61df 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -59,7 +59,7 @@ module ActiveRecord class ConnectionPool attr_accessor :automatic_reconnect attr_reader :spec, :connections - attr_reader :columns, :columns_hash, :primary_keys + attr_reader :columns, :columns_hash, :primary_keys, :tables # Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification # object which describes database connection information (e.g. adapter, @@ -84,12 +84,7 @@ module ActiveRecord @connections = [] @checked_out = [] @automatic_reconnect = true - - @tables = Hash.new do |h, table_name| - with_connection do |conn| - h[table_name] = conn.table_exists?(table_name) - end - end + @tables = {} @columns = Hash.new do |h, table_name| h[table_name] = with_connection do |conn| @@ -113,11 +108,22 @@ module ActiveRecord @primary_keys = Hash.new do |h, table_name| h[table_name] = with_connection do |conn| - @tables[table_name] ? conn.primary_key(table_name) : 'id' + table_exists?(table_name) ? conn.primary_key(table_name) : 'id' end end end + # A cached lookup for table existence + def table_exists?(name) + return true if @tables.key? name + + with_connection do |conn| + conn.tables.each { |table| @tables[table] = true } + end + + @tables.key? name + end + # Clears out internal caches: # # * columns -- cgit v1.2.3 From 1c6f4562d788cd5b63889b9597bc1765a1bd75e0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 8 Feb 2011 16:01:16 -0800 Subject: primary keys should not be cleared on cache clear, fixing oracle tests --- .../lib/active_record/connection_adapters/abstract/connection_pool.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index b1754e61df..4297c26413 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -128,12 +128,10 @@ module ActiveRecord # # * columns # * columns_hash - # * primary_keys # * tables def clear_cache! @columns.clear @columns_hash.clear - @primary_keys.clear @tables.clear end -- cgit v1.2.3