Playful MySQL 2: Transactions and some of its potential problems
2017-11-18 00:00
183 查看
摘要: MySQL transactions are business sequence commends that execute as a whole unit. This blog gives a brief introduction to MySQL transaction, and demonstrates two typical transaction problems: lost update and deadlock.
To write a transaction in MySQL:
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.
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
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.
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:
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:
In both command windows, type:
Open cmd1, type:
Open cmd2, type:
Open cmd1 again, type:
Open cmd2 again, type:
Now the report of deadlock shall be observed, as shown in the figure below.
Finally, type
to finish this demo and restore the database to proper status.
Note: You can use
to check your current status of automatic commition.
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.In both command windows, type:
SET AUTOCOMMIT = FALSE;
Open cmd1, type:
SELECT balance FROM Account WHERE id = 1 FOR UPDATE;
Open cmd2, type:
SELECT balance FROM Account WHERE id = 2 FOR UPDATE;
Open cmd1 again, type:
SELECT balance FROM Account WHERE id = 2 FOR UPDATE;
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.
相关文章推荐
- 本体开发的有关问题和命名规范——some Problems and Naming Conventions of Ontology development
- 本体开发的有关问题和命名规范——some Problems and Naming Conventions of Ontology development
- Ontology ---------- the definition of philosophy and in IT field
- Some examples of BLAS and LAPACK calls from C++.
- efcodefirst安装不了,This package (or one of its dependencies) contains PowerShell scripts and needs to
- Transactions and beyond it..
- Windows Live Writer Test and Some of My First Thoughts
- COM Interface Hooking and Its Application --- Tutorial of Designing & Implementing Program Interacting with MSN Messenger 6.0+ (
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log
- Answers to some common problems and questions about Xen
- High Performance Computing in Science and Engineering ' 05 : Transactions of the High Performance Co
- Some common problems in RAD and their solutions/work arounds
- html2canvas- Take Screenshot of Web Page and Save It to Server (Javascript and PHP)
- (Page 1 of 3 )A walking tour of JavaBeans What JavaBeans is, how it works, and why you want to use it
- General Structure of Quartz.NET and How To Implement It
- when compile /home/wangxiao/NVIDIA-CUDA-7.5 SAMPLES, it warning: gcc version larger than 4.9 not supported, so: old verson of gcc and g++ are needed
- c# dynamic,maybe I should say it is just some shortcut for "Object", box and unbox, without the cast
- all the characteristics of the Eagle Creek Centerline construct it a sensible buy and healthy value your money.
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log
- Some books recommended, and also a simple introduction of myself.