transactions are rolled back if thread is killed during transaction #382

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+26 −2
Split
@@ -213,8 +213,13 @@ def transaction(options = {})
decrement_open_transactions
begin
if open_transactions == 0
- commit_db_transaction
- commit_transaction_records
+ if Thread.current.status != 'aborting'
+ commit_db_transaction
+ commit_transaction_records
+ else
+ rollback_db_transaction
+ rollback_transaction_records(true)
+ end
else
release_savepoint
save_point_records = @_current_transaction_records.pop
@@ -619,3 +619,22 @@ def test_transaction_isolation__read_committed
end
end
end
+
+if (! defined?(RUBY_ENGINE) or RUBY_ENGINE == 'ruby' or RUBY_ENGINE == 'rbx') and RUBY_VERSION < '1.9'
+ class ThreadKillSafeTransactionTest < TransactionTest
+ def test_transactions_are_rolledback_when_thread_is_killed
+ dev = Developer.find(1)
+ original_salary = dev.salary
+ t = Thread.new do
+ Developer.transaction do
+ dev.update_attribute :salary, original_salary+50_000
+ sleep 10
+ dev.update_attribute :salary, 1 # never get here
+ end
+ end
+ sleep 2
+ t.kill
+ assert_equal original_salary, dev.reload.salary
+ end
+ end
+end