当前位置:网站首页>Transaction overview of tidb
Transaction overview of tidb
2022-06-27 06:35:00 【Tianxiang shop】
TiDB Support complete distributed transactions , Provide Optimistic business And Pessimism (TiDB 3.0 Introduction in ) Two transaction models . This article mainly introduces the statements involving transactions 、 Optimistic affairs and pessimistic affairs 、 The isolation level of the transaction , And optimistic transaction application side retry and error handling .
General statement
This chapter is introduced in TiDB How to use . The following example will be used to demonstrate the control flow of a simple transaction :
Bob To give Alice Transfer accounts 20 Yuan , There are at least two operations :
- Bob The accounts are down 20 element .
- Alice Account increase 20 element .
Transactions can ensure that either of the above two operations are successful , Or they all fail , No money will disappear or appear in vain .
Use bookshop In the database users surface , Insert some sample data into the table :
INSERT INTO users (id, nickname, balance) VALUES (2, 'Bob', 200); INSERT INTO users (id, nickname, balance) VALUES (1, 'Alice', 100);
Now? , Run the following transactions and explain the meaning of each statement :
BEGIN; UPDATE users SET balance = balance - 20 WHERE nickname = 'Bob'; UPDATE users SET balance = balance + 20 WHERE nickname= 'Alice'; COMMIT;
After the above transaction succeeds , The table shall be as follows :
+----+--------------+---------+ | id | account_name | balance | +----+--------------+---------+ | 1 | Alice | 120.00 | | 2 | Bob | 180.00 | +----+--------------+---------+
Open transaction
To explicitly open a new transaction , Both available BEGIN sentence , You can also use START TRANSACTION sentence , They have the same effect . grammar :
BEGIN;
START TRANSACTION;
TiDB The default transaction mode for is pessimistic transaction , You can also specify the open Optimistic business :
BEGIN OPTIMISTIC;
Turn on Pessimism :
BEGIN PESSIMISTIC;
If the above statement is executed , At present Session Is in the middle of a transaction , The system will automatically commit the current transaction first , Start a new transaction .
Commit transaction
COMMIT Statement for commit TiDB All changes made in the current transaction . grammar :
COMMIT;
Before enabling optimistic transactions , Please make sure that the application can handle COMMIT Statement may return an error . If you are not sure how the application will handle , It is recommended to use pessimistic transactions instead .
Roll back the transaction
ROLLBACK Statement is used to rollback and undo all modifications of the current transaction . grammar :
ROLLBACK;
Back to the previous transfer example , Use ROLLBACK After rolling back the entire transaction ,Alice and Bob The balance of has not changed , All modifications of the current transaction are canceled together .
TRUNCATE TABLE `users`; INSERT INTO `users` (`id`, `nickname`, `balance`) VALUES (1, 'Alice', 100), (2, 'Bob', 200); SELECT * FROM `users`; +----+--------------+---------+ | id | nickname | balance | +----+--------------+---------+ | 1 | Alice | 100.00 | | 2 | Bob | 200.00 | +----+--------------+---------+ BEGIN; UPDATE `users` SET `balance` = `balance` - 20 WHERE `nickname`='Bob'; UPDATE `users` SET `balance` = `balance` + 20 WHERE `nickname`='Alice'; ROLLBACK; SELECT * FROM `users`; +----+--------------+---------+ | id | nickname | balance | +----+--------------+---------+ | 1 | Alice | 100.00 | | 2 | Bob | 200.00 | +----+--------------+---------+
If the client connection is aborted or closed , The transaction will also be rolled back automatically .
Transaction isolation level
Transaction isolation level is the basis of database transaction processing ,ACID Medium “I”, namely Isolation, It refers to the isolation of transactions .
SQL-92 The standard defines 4 Kind of isolation level : Read uncommitted (READ UNCOMMITTED)、 Read submitted (READ COMMITTED)、 Repeatable (REPEATABLE READ)、 Serialization (SERIALIZABLE). For details, see table below. :
| Isolation Level | Dirty Write | Dirty Read | Fuzzy Read | Phantom |
|---|---|---|---|---|
| READ UNCOMMITTED | Not Possible | Possible | Possible | Possible |
| READ COMMITTED | Not Possible | Not possible | Possible | Possible |
| REPEATABLE READ | Not Possible | Not possible | Not possible | Possible |
| SERIALIZABLE | Not Possible | Not possible | Not possible | Not possible |
TiDB Syntax supports setting READ COMMITTED and REPEATABLE READ Two isolation levels :
mysql> SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; ERROR 8048 (HY000): The isolation level 'READ-UNCOMMITTED' is not supported. Set tidb_skip_isolation_level_check=1 to skip this error mysql> SET TRANSACTION ISOLATION LEVEL READ COMMITTED; Query OK, 0 rows affected (0.00 sec) mysql> SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; Query OK, 0 rows affected (0.00 sec) mysql> SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; ERROR 8048 (HY000): The isolation level 'SERIALIZABLE' is not supported. Set tidb_skip_isolation_level_check=1 to skip this error
TiDB Implemented snapshot isolation (Snapshot Isolation, SI) Consistency of levels . As with the MySQL bring into correspondence with , It's also called “ Repeatable ”. This isolation level is different from ANSI Repeatable read isolation level and MySQL Repeatable read isolation level . For more details, please read TiDB Transaction isolation level .
边栏推荐
- AHB2APB桥接器设计(2)——同步桥设计的介绍
- 多线程带来的的风险——线程安全
- Instance tunnel use
- Block level elements & inline elements
- 技术人员创业一年心得
- [QT notes] simple understanding of QT meta object system
- Once spark reported an error: failed to allocate a page (67108864 bytes), try again
- [getting started] regular expression Basics
- [cultivation system] common regular expressions
- Ora-00909: invalid number of parameters, caused by concat
猜你喜欢

Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)

Multithreading basic part2

G1 and ZGC garbage collector
Software testing year end summary report template

Run opcua protocol demo on raspberry pie 4B to access kubeedge

JS to implement bidirectional data binding

426-二叉树(513.找树左下角的值、112. 路径总和、106.从中序与后序遍历序列构造二叉树、654. 最大二叉树)

JVM对象组成和存储

Fast implementation of thread mesh networking

多线程带来的的风险——线程安全
随机推荐
主动学习(active learning)
winow10安装Nexus nexus-3.20.1-01
HTAP 快速上手指南
Scala advanced_ Member access modifier
快速实现蓝牙iBeacn功能详解
When there are multiple El select, the selected value is filtered by El select, and the last selected value is filtered by the second El select
Ora-00909: invalid number of parameters, caused by concat
426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
Multithreading basic part part 1
JS to implement bidirectional data binding
机 器 学 习
2018 mathematical modeling competition - special clothing design for high temperature operation
爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)
The risk of multithreading -- thread safety
tar: /usr/local:归档中找不到tar: 由于前次错误,将以上次的错误状态退出
古典密码体制--代换和置换
Quick personal site building guide using WordPress
信息系统项目管理师---第七章 项目成本管理
Tidb database Quick Start Guide
力扣 179、最大数