Giter Site home page Giter Site logo

lock-issues's Introduction

预备知识

隔离级别(低到高)

  • Read Uncommitted
  • Read Committed (Oracle, PostgreSQL, SQLServer...)
  • Repeatable Read (MySQL)
  • Serializable

性能要求:级别越低越好;数据一致性要求:级别越高越好

MySQL默认级别:Repeatable Read

mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.01 sec)

先调整到:Read Committed

mysql> set transaction_isolation='read-committed';
Query OK, 0 rows affected (0.00 sec)

mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.00 sec)

乐观锁

正确输出

余额:1000
[Transfer]查询到存款余额: 1000
[Withdraw]查询到存款余额: 1000
[Transfer]开始事务
[Transfer]存入100,余额变成 1100
[Transfer]提交事务
[Withdraw]开始事务
[Withdraw]已被transfer转账事务修改,需要重新执行(stale object error)
[Withdraw]查询到存款余额: 1100
[Withdraw]开始事务
[Withdraw]取出100,余额变成: 1000
[Withdraw]提交事务
余额: 1000

错误输出

余额:1000
[Transfer]查询到存款余额: 1000
[Withdraw]查询到存款余额: 1000
[Transfer]开始事务
[Transfer]存入100,余额变成 1100
[Transfer]提交事务
[Withdraw]开始事务
[Withdraw]取出100,余额变成: 900
[Withdraw]提交事务
余额: 900

死锁

输出

[TransB]开始事务
[TransB]锁住ord.id=1这条记录
[TransA]开始事务
[TransA]锁住customer.id=1这条记录
[TransB]等待TransA释放锁
[TransA]Error 1213: Deadlock found when trying to get lock; try restarting transaction
[TransA]提交事务
[TransB]提交事务

lock-issues's People

Contributors

guobinqiu avatar

Stargazers

 avatar

Watchers

James Cloos avatar  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.