From 9296e6939bcc786149a07dac334267c4035b623a Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 14 Sep 2012 17:30:37 +0100 Subject: Store the transaction number in the transaction object This avoids us having to manually increment and decrement it. --- .../abstract/database_statements.rb | 1 - .../connection_adapters/abstract/transaction.rb | 29 ++++++++++++++++++---- .../connection_adapters/abstract_adapter.rb | 10 +++++--- .../connection_adapters/postgresql_adapter.rb | 1 - .../lib/active_record/railties/console_sandbox.rb | 2 -- 5 files changed, 30 insertions(+), 13 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index 7cfaf3b0e5..5eb0f0f132 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -198,7 +198,6 @@ module ActiveRecord @transaction_joinable = last_transaction_joinable if outside_transaction? - @open_transactions = 0 @transaction = Transactions::Closed.new(self) elsif @transaction.open? && transaction_open begin diff --git a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb index 0a4649ffa7..4f1f9223ec 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb @@ -10,6 +10,10 @@ module ActiveRecord end class Closed < State + def number + 0 + end + def begin Open.new(connection, self) end @@ -33,16 +37,31 @@ module ActiveRecord def initialize(connection, parent) super connection - @parent = parent - @records = [] + @parent = parent + @records = [] + @finishing = false if parent.open? connection.create_savepoint else connection.begin_db_transaction end + end + + def number + if finishing? + parent.number + else + parent.number + 1 + end + end - connection.increment_open_transactions + # Database adapters expect that #open_transactions will be decremented + # before we've actually executed a COMMIT or ROLLBACK. This is kinda + # annoying, but for now we use this @finishing flag to toggle what value + # #number should return. + def finishing? + @finishing end def begin @@ -50,7 +69,7 @@ module ActiveRecord end def rollback - connection.decrement_open_transactions + @finishing = true if parent.open? connection.rollback_to_savepoint @@ -63,7 +82,7 @@ module ActiveRecord end def commit - connection.decrement_open_transactions + @finishing = true begin if parent.open? diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index f7cb88331d..b7fcf0e512 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -4,6 +4,7 @@ require 'bigdecimal/util' require 'active_support/core_ext/benchmark' require 'active_record/connection_adapters/schema_cache' require 'monitor' +require 'active_support/deprecation' module ActiveRecord module ConnectionAdapters # :nodoc: @@ -69,7 +70,6 @@ module ActiveRecord @instrumenter = ActiveSupport::Notifications.instrumenter @last_use = false @logger = logger - @open_transactions = 0 @pool = pool @query_cache = Hash.new { |h,sql| h[sql] = {} } @query_cache_enabled = false @@ -237,14 +237,16 @@ module ActiveRecord @connection end - attr_reader :open_transactions + def open_transactions + @transaction.number + end def increment_open_transactions - @open_transactions += 1 + ActiveSupport::Deprecation.warn "increment_open_transactions is deprecated and has no effect" end def decrement_open_transactions - @open_transactions -= 1 + ActiveSupport::Deprecation.warn "decrement_open_transactions is deprecated and has no effect" end def transaction_joinable=(joinable) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index d1751d70c6..76198b42ff 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -433,7 +433,6 @@ module ActiveRecord def reconnect! clear_cache! @connection.reset - @open_transactions = 0 configure_connection end diff --git a/activerecord/lib/active_record/railties/console_sandbox.rb b/activerecord/lib/active_record/railties/console_sandbox.rb index 65a3d68619..90b462fad6 100644 --- a/activerecord/lib/active_record/railties/console_sandbox.rb +++ b/activerecord/lib/active_record/railties/console_sandbox.rb @@ -1,6 +1,4 @@ -ActiveRecord::Base.connection.increment_open_transactions ActiveRecord::Base.connection.begin_db_transaction at_exit do ActiveRecord::Base.connection.rollback_db_transaction - ActiveRecord::Base.connection.decrement_open_transactions end -- cgit v1.2.3