Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

expose :remember_record_state => false option on transaction(), so ap…

…plications that don't need state rollback (or after_commit/rollback hooks) can garbage collect the records during long transactions
  • Loading branch information...
commit a45b371e6bb3b3312ee1b29f925bdc2d01e0af1c 1 parent af9f99a
@willbryant authored
View
9 activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -164,7 +164,7 @@ def supports_statement_cache?
# end # RELEASE SAVEPOINT active_record_1 <--- BOOM! database error!
# end
def transaction(options = {})
- options.assert_valid_keys :requires_new, :joinable
+ options.assert_valid_keys :requires_new, :joinable, :remember_record_state
last_transaction_joinable = defined?(@transaction_joinable) ? @transaction_joinable : nil
if options.has_key?(:joinable)
@@ -172,6 +172,11 @@ def transaction(options = {})
else
@transaction_joinable = true
end
+ if options.has_key?(:remember_record_state)
+ remember_record_state = options[:remember_record_state]
+ else
+ remember_record_state = true
+ end
requires_new = options[:requires_new] || !last_transaction_joinable
transaction_open = false
@@ -187,7 +192,7 @@ def transaction(options = {})
end
increment_open_transactions
transaction_open = true
- @_current_transaction_records.push([])
+ @_current_transaction_records.push(remember_record_state ? [] : nil)
end
yield
end
View
23 activerecord/test/cases/transactions_test.rb
@@ -389,6 +389,29 @@ def test_restore_active_record_state_for_all_records_in_a_transaction
assert !@second.destroyed?, 'not destroyed'
end
+ def test_optional_no_restore_active_record_state_so_records_not_retained_in_memory
+ topic_1 = Topic.new(:title => 'test_1')
+ Topic.transaction(:remember_record_state => false) do
+ assert topic_1.save
+ @first.destroy
+ assert topic_1.persisted?, 'persisted'
+ assert_not_nil topic_1.id
+ assert @first.destroyed?, 'destroyed'
+ raise ActiveRecord::Rollback
+ end
+
+ assert topic_1.persisted?, 'state was rolled back, so record must have been retained'
+ assert_not_nil topic_1.id
+ assert_raises(ActiveRecord::RecordNotFound) do
+ topic_1.reload
+ flunk 'database was not rolled back'
+ end
+ assert @first.destroyed?, 'state was rolled back, so record must have been retained'
+ assert_nothing_raised do
+ Topic.find(@first.id)
+ end
+ end
+
if current_adapter?(:PostgreSQLAdapter) && defined?(PGconn::PQTRANS_IDLE)
def test_outside_transaction_works
assert Topic.connection.outside_transaction?
Please sign in to comment.
Something went wrong with that request. Please try again.