Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

transactions are rolled back if thread is killed during transaction

commit 7f8ac269578e847329c7cfb2010961d3e447fd19 1 parent 635abc7
@coderrr authored
View
9 activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -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
View
19 activerecord/test/cases/transactions_test.rb
@@ -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

1 comment on commit 7f8ac26

@baash05

Shouldn't the ensure be all rollback too?

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