Giter Club home page Giter Club logo

mysql-disconnect-test's Introduction

a simple python script to emulate the DB connection crash

When the process holds the row lock crashes without commit or closing the connection how doe MySQL detect that and release the row lock ?

I had though there maybe existed some kind time out until MySQL detects that, so I wrote this simple python script to test it. But to my surprise I find the second process grabs the lock immediately after the first process returns, i.e. no time out from I have observed. But how does MySQL detect the lost connection and release the row lock immediately ?

Use SELECT … FOR UPDATE to test the row lock behavior

To check how long will the second thread/process wait if the first one crashes, please refer to the following 2 discussions

MySQL rollback on transaction with lost/disconnected connection

When a thread/process crashes on acquired DB locks, how does DB detects that and releases the lock?

Host MySQL on https://www.freemysqlhosting.net/, create a table like following

mysql> select * from users;
+----+-------------+
| id | username    |
+----+-------------+
|  1 | john doe    |
|  2 | jane doe    |
|  3 | alice jones |
|  4 | lisa romero |
+----+-------------+

Using https://pypi.org/project/mysql-connector-python/ as mysql driver.

Open 2 terminals, in the first terminal run ./crash_test.py -c 1 return without calling close() to emulate the crash

in the second termial ./crash_test.py to select and update. I found when the 1st termial return, the second terminal will continue immediately.

BTW, innodb_lock_wait_timeout is easy to triggered if just sleep(90)

What I observered contradicts the answers

But what I see so far is that if the first process gets the row lock exits without commit & close the connection, the second process will immediately get the lock and continue to update the record. I don't see any wait_timeout invloved.

What I obseve is that:

  1. If the first process gets the lock commit, the second process will get the lock immediately
  2. If the first process gets the lock just return withoutcommit & close, the second process will get the lock immediately too.

This seems to contradict the answers in MySQL rollback on transaction with lost/disconnected connection

So the question now is HOW MySQL does that ?

mysql-disconnect-test's People

Contributors

qiulang avatar danielmewes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.