-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Records created outside of transaction block gets rolled back when transaction raises exception #2598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think this is the default behaviour with |
Nested transactions are very tricky, especially with their (subjectively) nonsensical defaults.
Exception handling and rolling back:
The above statement covers the behaviour you're observing with When you rescue the exception inside the Side note:
|
Thanks for comments. Now it's making sense to me. It didn't occur to me at all that my Turning off transactional tests (I've chosen
|
Hi, yes without transactional tests you will need another database cleaning solution such as database_cleaner. As this is resolved by turning off transactional tests confirming it was the nested transactions causing the issue, I've closed this issue. The transactions are managed all by Rails test helpers. |
I'd suggest staying away from I'd keep the code the way it was, rescuing the deadlock exception inside the inner transaction. Specifically for your problem, retrying of deadlocks, there's |
Yes I agree with everything you've mentioned. Thanks for quick advices! It was a great lesson for me that I can take advantage of hereafter 👍 |
What Ruby, Rails and RSpec versions are you using?
Observed behaviour
When the application code raises exception in
.transaction
, records created outside of its block, including test data, got rolled back.Expected behaviour
Raising exception in
.transaction
block rolls back only the data created in the.transaction
.Background
Although I'm attaching the reproduction repo below, I bet it doesn't look very straightforward. So let me explain why here.
I came across this behavior while I was struggling with a method which looked like this (but far larger): sato11/rspec-rails-transaction-sample@169a98a. I wanted it to become this: sato11/rspec-rails-transaction-sample@5f8d90b. Because on detecting deadlock our innoDB rolls back the transaction (doc), we needed to not only rescue it but also explicitly
BEGIN
again. However when I wrapped.transaction
inbegin-end
the test started to fail and that's what I'm reporting here.I'm not really sure if it's somehow related to
#use_transactional_fixtures
, but I bet it's a very subtle one. I'd be glad if I could get any advice of how I can work on this or how I can write the test in a different way 🙏Can you provide an example app?
Sure.
https://github.com/sato11/rspec-rails-transaction-sample
The text was updated successfully, but these errors were encountered: