为什么80%的码农都做不了架构师?>>>   

A Brief Introduction to Transaction in MySQL

Transactions has 4 properties called "ACID". (Atomicity, Consistency, Isolation and Durability)

To write a transaction in MySQL:

SET AUTOCOMMIT = FALSE;BEGIN;# Or: START TRANSACTION;/*PUT TRANSACTION HERE*/COMMIT;# OR ROLLBACK;SET AUTOCOMMIT = TRUE;

If committed, then the results will be written into the disk by buffer pool, if rolled back, then the changes in buffer pools are cleaned and no matter what happened, those changes in buffer bool, disks and query compilers about the transaction shall all be traceable in database log.

Demo of Potential Transaction Problems using Windows cmd

The demonstration schema names "transac_demo". It has an account table whose structure is:

/* set up the database */
DROP TABLE IF EXISTS Account;
CREATE TABLE Account (Id int, AccountType char(10), Balance decimal(10,2), primary key(Id));
INSERT INTO Account values
(1, 'savings', 5000), (2, 'credit', 1000);

To use Windows command line to demonstrate transaction, open the directory of the "bin" file of the MySQL server, whose default path is "C:\Program Files\MySQL\MySQL Server 5.7\bin". Then type

mysql -u USERNAME -p -h HOSTNAME SCHEMA

Usually, for local database retrieval, USERNAME is root while HOSTNAME is localhost, SCHEMA in this case is transac_demo, if logged in successfully, it should look like the figure shown below.

By typing "quit", or simply close the window, to log out the client program.

Lost Update

Lost update could happen when different users are reading and writing (reader 1 reads before writer 2 writes and then reader 1 writes) to the same piece of data. To see how this problem occurs, open two command line windows and log into the server for each of them, then perform jobs as seen below consequently:

Both of the "users" (simulated by 2 command lines) in this case are taking money out from a same account (say, in a case of a company account where 2 account managers are taking money). The result thus should be 4800. But after both have finished, the result is still 4900, in this case, one of the update got "lost" and this is called the lost update problem.

The solution to this is to use an "exclusive lock". Code in both sides are:

SET AUTOCOMMIT = FALSE;
BEGIN;
/*An exclusive lock is used, this is row level.*/
SELECT balance FROM Account WHERE id = 1 INTO @bal FOR UPDATE;
SET @newbal = @bal - 100;
UPDATE Account SET balance = @newbal WHERE id = 1;
COMMIT;
SET AUTOCOMMIT = TRUE;

As shown in the figure below.

User 2 (simulated as the green cmd) waits until user 1 (simulated as black cmd) finishes update, or time out (60 seconds). Otherwise it is keep locked as can be seen. Only when user 1 finishes update can user 2 then execute update. One successful transaction of user 1 and 2 can be shown as below:

Deadlock

Deadlock happens when user 1 is holding resource 1 and requesting for resource 2 while user 2 is holding resource 2 while requesting for resource 1. This might also happen in database. To see this, following the steps below.

  1. In both command windows, type:
SET AUTOCOMMIT = FALSE;
  1. Open cmd1, type:
SELECT balance FROM Account WHERE id = 1 FOR UPDATE;
  1. Open cmd2, type:
SELECT balance FROM Account WHERE id = 2 FOR UPDATE;
  1. Open cmd1 again, type:
SELECT balance FROM Account WHERE id = 2 FOR UPDATE;
  1. Open cmd2 again, type:
SELECT balance FROM Account WHERE id = 1 FOR UPDATE;

Now the report of deadlock shall be observed, as shown in the figure below.

Finally, type

SET AUTOCOMMIT = TRUE;

to finish this demo and restore the database to proper status.

Note: You can use

SELECT @@autocommit;

to check your current status of automatic commition.

转载于:https://my.oschina.net/Samyan/blog/1575453

Playful MySQL 2: Transactions and some of its potential problems相关推荐

  1. CSV格式数据如何导入MySQL?

    经常有客户咨询如何将CSV文件导入到MySQL数据库中,特写此文介绍一种方便.快捷的方法. 我们要使用的辅助工具是著名的MySQL管理软件:Navicat for MySQL 1)我准备了一个字符编码 ...

  2. Oracle 数据库、Microsoft SQL Server、MySQL 数据库三种常见数据库的区别深度剖析

    文章目录 前言 一.ORACLE 数据库 二.Microsoft SQL Server 数据库 三.MySQL 数据库 总结 前言 Oracle 数据库.Microsoft SQL Server.My ...

  3. mysql的主要指标_mysql 主要性能指标

    1.mysql connections->threads cached->threads cached mysql connections->threads cached->t ...

  4. MySQL中的事务及读写锁实现并发访问控制

    一.并发控制中锁的概念 锁是并发控制中最核心的概念之一,在MySQL中的锁分两大类,一种是读锁,一种是写锁,读锁也可以称为共享锁(shared lock),写锁也通常称为排它锁(exclusive l ...

  5. SQL Server和MySQL数据库

    导读:接下来的网上商城的项目,需要用到MySQL数据库了.这个对于我来说,是一个新接触的东西,按照惯例,在刚开始学习一个东西的时候,先从宏观上去了解它.本篇博客,先介绍SQL Server的基本内容, ...

  6. sql server,mysql,oracle的区别

    Oracle Oracle 能在所有主流平台上运行(包括Windows).完全支持所有的工业标准.采用完全开放策略.可以使客户选择最适合的解决方案.对开发商全力支持,Oracle并行服务器通过使一组结 ...

  7. sqlite mysql_两款主流数据库对比,SQLite和MySQL哪款是你的菜?

    数据库是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅仅是存储和管理数据,而转变成用户所需要的各种数据管理的方式 ...

  8. mysql 指南_MySQL-快速指南

    mysql 指南 MySQL-快速指南 (MySQL - Quick Guide) MySQL-简介 (MySQL - Introduction) 什么是数据库? (What is a Databas ...

  9. 【数据库】mysql移植

    一.源码下载 1.下载mysql源码 源码下载地址:选择版本:5.1.72(这是个老版本,高版本需要使用cmake) https://cdn.mysql.com/archives/mysql-5.1/ ...

  10. mysql dba系统学习(4)mysql的多实例multi启动停止

    mysql的多实例配置安装 一,什么情况下我们会考虑一台物理服务器上部署多个实例,大致有以下几种情况: 1,采用了数据伪分布式架构的原因,而项目启动初期又不一定有那多的用户量,为此先一组物理数据库服务 ...

最新文章

  1. c++类例子之类中有类
  2. /lib/lsb/init-functions
  3. Leetcode中单链表题总结
  4. 世界杯直播背后:腾讯云极速高清技术部署实录
  5. WS-Eventing、WS-Transfer Web服务标准
  6. Easyconnect mac版本下载地址
  7. logstash 启动方式
  8. 论文实录 | 毕业设计如何做需求分析?
  9. 数据可视化工具在医疗领域的应用
  10. Python统计列表元素出现次数
  11. android 智能识别名片,小程序云开发实战:实现 AI 智能名片识别小程序
  12. Android——打电话(选择指定手机卡)、发短信
  13. RT-Thread柿饼控件(1)-- AnimatedImage
  14. 模型误差、观测误差、截断误差(或称方法误差)、舍入误差
  15. Swift强大的数组
  16. Dev C++的安装以及基本使用方法
  17. 对C语言的关键字及部分关键字用法的简单理解
  18. 以太网帧、IP 帧、UDP/TCP帧、http 报文结构解析
  19. 【window10】解决任务管理器有进程无法强制结束情况
  20. arcgis新建图层信息复制_怎么在arcgis中把一个图层复制到另一个图层上

热门文章

  1. 我又拖后腿了, 2月全国程序员平均工资13716元!
  2. 5gh掌上云计算认证不通过_华为云计算认证含金量高么?
  3. 高效能人士的七个习惯-第二章-阅读
  4. JSON_EXTRACT JSON_UNQUOTE以及json数组下标选择
  5. 小红花代表什么_《送你一朵小红花》:细品后才知道,结尾处的平行世界代表着什么...
  6. 算法题 高斯消元解线性方程组(Python)
  7. 主板rgb接口是什么_什么是主板?
  8. JS实现中文转拼音首字母和五笔简码
  9. cvc降噪和主动降噪_音频知识:CVC降噪和ANC主动降噪的区别和应用
  10. 如果让markdown的图片变清晰/改变大小