Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

transactions are rolled back if thread is killed during transaction

  • Loading branch information...
commit 7f8ac269578e847329c7cfb2010961d3e447fd19 1 parent 635abc7
coderrr authored May 02, 2011
9  activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -213,8 +213,13 @@ def transaction(options = {})
213 213
           decrement_open_transactions
214 214
           begin
215 215
             if open_transactions == 0
216  
-              commit_db_transaction
217  
-              commit_transaction_records
  216
+              if Thread.current.status != 'aborting'
  217
+                commit_db_transaction
  218
+                commit_transaction_records
  219
+              else
  220
+                rollback_db_transaction
  221
+                rollback_transaction_records(true)
  222
+              end
218 223
             else
219 224
               release_savepoint
220 225
               save_point_records = @_current_transaction_records.pop
19  activerecord/test/cases/transactions_test.rb
@@ -619,3 +619,22 @@ def test_transaction_isolation__read_committed
619 619
     end
620 620
   end
621 621
 end
  622
+
  623
+if (! defined?(RUBY_ENGINE) or RUBY_ENGINE == 'ruby' or RUBY_ENGINE == 'rbx') and RUBY_VERSION < '1.9' 
  624
+  class ThreadKillSafeTransactionTest < TransactionTest
  625
+    def test_transactions_are_rolledback_when_thread_is_killed
  626
+      dev = Developer.find(1)
  627
+      original_salary = dev.salary
  628
+      t = Thread.new do
  629
+        Developer.transaction do
  630
+          dev.update_attribute :salary, original_salary+50_000
  631
+          sleep 10
  632
+          dev.update_attribute :salary, 1 # never get here
  633
+        end
  634
+      end
  635
+      sleep 2
  636
+      t.kill
  637
+      assert_equal original_salary, dev.reload.salary
  638
+    end
  639
+  end
  640
+end

1 note on commit 7f8ac26

David Rawk

Shouldn't the ensure be all rollback too?

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