Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove the transaction_open variable

  • Loading branch information...
commit 748052a99bb3046e4cdb7f8c00457da74fbdb75b 1 parent 2805875
@jonleighton jonleighton authored
View
49 activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -3,7 +3,7 @@ module ConnectionAdapters # :nodoc:
module DatabaseStatements
def initialize
super
- @transaction = ClosedTransaction.new(self)
+ reset_transaction
end
# Converts an arel AST to SQL
@@ -172,37 +172,40 @@ def supports_statement_cache?
def transaction(options = {})
options.assert_valid_keys :requires_new, :joinable
- transaction_open = false
-
- begin
- if options[:requires_new] || !current_transaction.joinable?
- begin_transaction(options)
- transaction_open = true
- end
-
- yield
- rescue Exception => error
- if !outside_transaction? && transaction_open
- rollback_transaction
- transaction_open = false
- end
-
- raise unless error.is_a?(ActiveRecord::Rollback)
+ if !options[:requires_new] && current_transaction.joinable?
+ within_existing_transaction { yield }
+ else
+ within_new_transaction(options) { yield }
end
+ rescue Exception => error
+ raise unless error.is_a?(ActiveRecord::Rollback)
+ end
+ def within_new_transaction(options = {}) #:nodoc:
+ begin_transaction(options)
+ yield
+ rescue Exception => error
+ rollback_transaction unless outside_transaction?
+ raise
ensure
if outside_transaction?
- @transaction = ClosedTransaction.new(self)
- elsif current_transaction.open? && transaction_open
+ reset_transaction
+ else
begin
- commit_transaction
- rescue Exception
+ commit_transaction unless error
+ rescue Exception => e
rollback_transaction
raise
end
end
end
+ def within_existing_transaction #:nodoc:
+ yield
+ ensure
+ reset_transaction if outside_transaction?
+ end
+
def current_transaction #:nodoc:
@transaction
end
@@ -225,6 +228,10 @@ def rollback_transaction #:nodoc:
@transaction = @transaction.rollback
end
+ def reset_transaction
+ @transaction = ClosedTransaction.new(self)
+ end
+
# Register a record with the current transaction so that its after_commit and after_rollback callbacks
# can be called.
def add_transaction_record(record)
View
5 activerecord/test/cases/transactions_test.rb
@@ -423,6 +423,11 @@ def test_open_transactions_count_is_reset_to_zero_if_no_transaction_active
assert_equal 0, Topic.connection.open_transactions
end
assert_equal 0, Topic.connection.open_transactions
+
+ Topic.transaction do
+ Topic.connection.rollback_db_transaction
+ end
+ assert_equal 0, Topic.connection.open_transactions
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.