This project is archived and is in readonly mode.
ActiveRecord::Rollback Does Not Work In Nested Transactions
-
Michael Koziarski
Yeah, that's not possible, and can't be possible so the docs need updating.
If you raise another exception it'll rollback, but obviously AR::Rollback doesn't, and shouldn't, propagate.
-
Ken Collins
Agreed, it was looking very hackish in my code attempts to implement this. And indeed the raising of some other exception does do the job.
I'll add the doc changes to support this and submit a patch soon.
-
Pratik
Hey Ken,
Just lemme know your github username and I'll give you commit access to docrails.
-
Ken Collins
Ah duh, I think I might already be on, it's "metaskills".
-
Pratik
Yup, you're already there :)
-
Ken Collins
Thanks to whomever got that in docrails, I've been a bit too busy lately.
-
Jason Yuen
- Tag changed from 2.3, activerecord, nested, rollback, transaction to 2, activerecord, nested, rollback, transaction
@Michael: I was wondering about your comment that "obviously AR::Rollback doesn't, and shouldn't, propagate." We are nesting our calls to #transaction without using :require_new. In this situation, we are observing that when we raise ActiveRecord::Rollback in our inner transaction block, the database transaction does not get rolled back.
User.transaction do User.create(:username => 'Kotori') User.transaction do User.create(:username => 'Nemu') raise ActiveRecord::Rollback end endIn this example, neither 'Kotori' nor 'Nemu' should exist in the database but they do. It seems like this is a bug so I've attached a patch (for 2.3.4 but it doesn't seem like the code has changed in master) which addresses this issue (unless of course, there is a reason why this isn't actually an issue). Basically, the rollback exception is always re-raised until we get to the outermost transaction block.
-
Jason Yuen
- Tag changed from 2, activerecord, nested, rollback, transaction to 2.3, activerecord, nested, rollback, transaction
-
Jason Yuen
- Tag changed from 2.3, activerecord, nested, rollback, transaction to 2, activerecord, nested, rollback, transaction
Sorry guys - I think I was a little overzealous with the patch and I hadn't fully thought through all the implications (e.g. how it affects the validation lifecycle). I have an operation in a nested transaction block that causes an ActiveRecord::StaleObjectError. Instead of letting the stale object error propagate, I was rescuing it and re-raising an ActiveRecord::Rollback, thinking that the rollback would propagate across transaction boundaries and rollback operations in outer transaction blocks. I guess I was just being lazy, hoping that I wouldn't have to explicitly handle the stale object error :)
Sorry again for the noise!
Jason
-
Jason Yuen
- Tag changed from 2, activerecord, nested, rollback, transaction to 2.3.x, activerecord, nested, rollback, transaction
-
Ivan Evtuhovich
You fix this only in docrails, but rdoc documentation is wrong (patch attached)
And i wonder why by default nested transaction rollback do not works?
