Skip to content
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...
1 parent b9d36bd commit 1372be17a157c72163ccd72debfd97140baa8368 @willbryant committed Oct 11, 2011
View
9 activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -111,14 +111,19 @@ def outside_transaction?
# 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)
@transaction_joinable = options[:joinable]
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
@@ -134,7 +139,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
@@ -368,6 +368,29 @@ def test_restore_active_record_state_for_all_records_in_a_transaction
assert_equal false, @second.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?

0 comments on commit 1372be1

Please sign in to comment.
Something went wrong with that request. Please try again.