fix(mysql, mariadb): release connection on deadlocks#12841
fix(mysql, mariadb): release connection on deadlocks#12841papb merged 2 commits intosequelize:mainfrom
Conversation
4f99328 to
c900b71
Compare
Codecov Report
@@ Coverage Diff @@
## master #12841 +/- ##
==========================================
- Coverage 96.44% 96.32% -0.13%
==========================================
Files 95 95
Lines 9154 9262 +108
Branches 0 90 +90
==========================================
+ Hits 8829 8922 +93
+ Misses 325 323 -2
- Partials 0 17 +17
Continue to review full report at Codecov.
|
|
Thanks for the prompt review! I improved the comments and pushed some new changes. @papb - feel free to take another look |
079a7bc to
0c1b449
Compare
|
@papb - quick ping on this - can you take a look? We're currently using our own branch, and would love to get this checked in so we can get back to the normal releases. |
|
Quick follow-up ping! @papb - thanks again! |
|
Looks like this is more robust than #12588 |
|
Hello!! Thank you very much for this super high quality PR. Great work! Sorry to take so long! |
|
🎉 This PR is included in version 6.5.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This is a follow-up for a problem not covered by #12841 This commit also contains some refactoring.
|
Thanks for this. I believe the scope of this fix includes postgres as well. We were seeing unreleased connections after deadlocks caused by non-transaction calls within transactions. |
|
Hi @arbieo, if I understand correctly you are saying this PR also fixed a similar postgres issue? Great news then :) |
|
@papb Correct. I made a tentative fix in my local for the issue we had and then found this online afterwards that is largely the same (I probably should have checked here first). |
This is a follow-up for a problem not covered by #12841.
* test(mysql, mariadb): improve transaction tests - Greatly improve test for `SELECT ... LOCK IN SHARE MODE` - Greatly improve test for deadlock handling * fix(mysql): release connection on deadlocks This is a follow-up for a problem not covered by #12841. * refactor(mariadb): `query.js` similar to mysql's * Update comments with a reference to this PR
* test(mysql, mariadb): improve transaction tests - Greatly improve test for `SELECT ... LOCK IN SHARE MODE` - Greatly improve test for deadlock handling * fix(mysql): release connection on deadlocks This is a follow-up for a problem not covered by sequelize#12841. * refactor(mariadb): `query.js` similar to mysql's * Update comments with a reference to this PR
* test(mysql, mariadb): improve transaction tests - Greatly improve test for `SELECT ... LOCK IN SHARE MODE` - Greatly improve test for deadlock handling * fix(mysql): release connection on deadlocks This is a follow-up for a problem not covered by sequelize#12841. * refactor(mariadb): `query.js` similar to mysql's * Update comments with a reference to this PR
Pull Request check-list
Please make sure to review and check all of these items:
npm run testornpm run test-DIALECTpass with this change (including linting)?Description of change
Closes #11571
This change makes deadlocked queries release the MariaDB/MySQL connection and does a redundant rollback. These ensure that subsequent queries work properly.
Debugging
While running sequelize in production for a collaborative document editor, we found that deadlocks on MariaDB would sometimes cause other queries to stall. After debugging, we realized that this is because MySQL/MariaDB has custom deadlock handling in Sequelize.
transaction.finished = 'rollback'in therunfunction of query.jstransaction()of sequelize.js,transaction.rollback()isn't called iftransaction.finishedis settransaction.cleanup(), which releases the connection is never called for this connection, and can cause hangs in this caseFix
The fix was to ensure we call
transaction.cleanup(), even if we don't rollback the transaction. I added a test for this case too.Interestingly, on MariaDB, we still need to do an explicit rollback query. Without it, a follow-up READ COMMITTED transaction wouldn't behave as expected. I added this to the testcase too.