Permalink
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 d731f5c commit 837b3696a7ecc3f73c8061c3822d797698a79240 @willbryant committed Oct 11, 2011
@@ -162,14 +162,19 @@ 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)
@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
@@ -185,7 +190,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
@@ -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?

0 comments on commit 837b369

Please sign in to comment.