-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
6.0.0rc1 - Foreign Keys in Transaction Deadlocks Database #36338
Comments
Update: I was searching the source around If you do something like class SecondaryBase < ApplicationRecord
self.abstract_class = true
connects_to database: { reading: :secondary, writing: :secondary }
end In general, it's probably a better idea to abstract your |
This is the correct way to do this. You really don't want to connect multiple models to the same database because you'll eventually run out of connections. There is a limit to the number of conns you can open in mysql. I think perhaps the error is confusing but makes sense in that you can't have multiple connections doing the same work. I'm sure if foreign keys weren't used we'd see another kind of error. I don't know if there would be an easy way to catch that it's not in a parent class...maybe we can check if I was planning on working on documentation this week and will be sure to mention that it should be used in a parent model and not more than once. |
For what it's worth, it wasn't clear to me that |
With multiple databases set up, if a model uses
connects_to
and has a foreign key, then any transaction will deadlock and timeout.Steps to reproduce
I've created an example app that shows this issue https://github.com/tuxagon/20190523-rails-multi-db-foreign-key-bug-example
The setup can be seen via the commits and I can walk through the steps here.
I set up the project to work with 2 databases and added
connects_to
on the models for the secondary database. Each database has a simple parent-child relationship with foreign keyssongs
belong toartists
(in primary)books
belong toauthors
(in secondary)The database connection is via MySQL InnoDB.
In the
test/models/song_test.rb
file, you will find that the test runs fine in the normal transactional test.In the
test/models/book_test.rb
file, you will find that the test will timeoutExpected behavior
Foreign key relationships should be adhered to within the scope of a transaction regardless of whether a model uses
connects_to
.Actual behavior
A model that uses
connects_to
and has a foreign key relationship will lock within a transaction.System configuration
Rails version: 6.0.0.rc1
Ruby version: 2.6.3
MySQL version: 5.6 & 5.7
As a bonus, I originally found this after encountering another issue in tests where the connection was leaking between tests on different models. I added
connects_to
on all my models to prevent the leakage. It appeared as if you didn't declare the connection explicitly, it used the last used connection.The text was updated successfully, but these errors were encountered: