rails/rails — Issue #3455: ActiveRecord::Rollback should propagate across nested, non-:require_new transaction blocks
Author: sskirby
Created: 2011-10-28 04:28:23+00:00
State: open

The current docs make reference to a strange situation that can occur:

(http://apidock.com/rails/ActiveRecord/Transactions/ClassMethods under Nested Transactions)

> ```Ruby
User.transaction do
User.create(:username => 'Kotori')
User.transaction do
User.create(:username => 'Nemu')
raise ActiveRecord::Rollback
end
end
```
> creates both “Kotori” and “Nemu”. Reason is the ActiveRecord::Rollback exception in the nested block does not issue a ROLLBACK. Since these exceptions are captured in transaction blocks, the parent block does not see it and the real transaction is committed.

Would it not make sense to have the ActiveRecord::Rollback propagate until it hits a transaction block that will cause a ROLLBACK? I think it would be much more consistent if an ActiveRecord::Rollback would always cause a ROLLBACK. We can always use the :require_new option to control how far an ActiveRecord::Rollback propagates.

It also looks like the current behaviour is 'fairly' recent: https://rails.lighthouseapp.com/projects/8994/tickets/1951-activerecordrollback-does-not-work-in-nested-transactions

I'm more than happy to create a patch with tests if I know that this is acceptable.