当前位置:网站首页>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 .
边栏推荐
- HTAP 深入探索指南
- Fast implementation of thread mesh networking
- 爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)
- 建模竞赛-光传送网建模与价值评估
- Keep 2 decimal places after multiplying SQLSEVER fields
- 高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression
- 力扣 179、最大数
- Force buckle 179, max
- 427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
- 2018年数学建模竞赛-高温作业专用服装设计
猜你喜欢

Meaning of 0.0.0.0:x

Block level elements & inline elements

JS to implement bidirectional data binding

AHB2APB桥接器设计(2)——同步桥设计的介绍

浅谈GPU:历史发展,架构

2018 mathematical modeling competition - special clothing design for high temperature operation

Caldera安装及简单使用

Proxy reflect usage details

使用CSDN 开发云搭建导航网站

Redis 缓存穿透、缓存击穿、缓存雪崩
随机推荐
记一次Spark报错:Failed to allocate a page (67108864 bytes), try again.
Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
On gpu: historical development and structure
Dev++ environment setting C language keyword display color
JVM类加载机制
力扣 179、最大数
My opinion on test team construction
高斯分布Gaussian distribution、线性回归、逻辑回归logistics regression
论文阅读技巧
多线程带来的的风险——线程安全
浅谈GPU:历史发展,架构
美摄云服务方案:专为轻量化视频制作场景打造
LeetCode 0086.分隔链表
Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)
Currying Scala functions
thrift
Active learning
yaml文件加密
Thinking technology: how to solve the dilemma in work and life?
Dev++ 环境设置C语言关键字显示颜色