深入浅出MySQL事务和锁定语句

news2024/12/23 16:07:19

https://dev.mysql.com/doc/refman/8.0/en/sql-transactional-statements.html

13.3事务和锁定语句

13.3.1启动事务、提交和回滚语句

开启事务

begin
START TRANSACTION

提交事务

COMMIT

回滚事务

ROLLBACK

查询自动提交

show SESSION VARIABLES where variable_name ="autocommit"
show GLOBAL VARIABLES where variable_name ="autocommit"

在这里插入图片描述
自动提交设置,0关闭,1开启

SET autocommit = 0

默认情况下,MySQL开启自动提交。没有显示开启事务的时候,每条语句都是原子的。

当显示开启事务时候,在提交或者回滚之前自动提交模式会被关闭。

13.3.2不能回滚的语句

13.3.3导致隐式提交的语句

13.3.4保存点、回滚到保存点和释放保存点语句

13.3.5锁定备份实例和解锁实例语句

13.3.6锁定表和解锁表语句

13.3.7设置事务语句

13.3.8 XA事务

隔离级别

From highest amount of consistency and protection to the least, the isolation levels supported by InnoDB are: SERIALIZABLE, REPEATABLE READ, READ COMMITTED, and READ UNCOMMITTED.

REPEATABLE READ 可重复读是 InnoDB 默认隔离级别。

READ UNCOMMITTED 读未提交

The isolation level that provides the least amount of protection between transactions. Queries employ a locking strategy that allows them to proceed in situations where they would normally wait for another transaction. However, this extra performance comes at the cost of less reliable results, including data that has been changed by other transactions and not committed yet (known as dirty read). Use this isolation level with great caution, and be aware that the results might not be consistent or reproducible, depending on what other transactions are doing at the same time. Typically, transactions with this isolation level only do queries, not insert, update, or delete operations.

See Also ACID, dirty read, isolation level, locking, transaction.

总结:读未提交会产生脏读(别的事务修改未提交的数据),隔离级别最低,性能最高,适用于只读场景。

READ COMMITTED 读已提交

An isolation level that uses a locking strategy that relaxes some of the protection between transactions, in the interest of performance. Transactions cannot see uncommitted data from other transactions, but they can see data that is committed by another transaction after the current transaction started. Thus, a transaction never sees any bad data, but the data that it does see may depend to some extent on the timing of other transactions.

When a transaction with this isolation level performs UPDATE … WHERE or DELETE … WHERE operations, other transactions might have to wait. The transaction can perform SELECT … FOR UPDATE, and LOCK IN SHARE MODE operations without making other transactions wait.

SELECT … FOR SHARE replaces SELECT … LOCK IN SHARE MODE in MySQL 8.0.1, but LOCK IN SHARE MODE remains available for backward compatibility.

See Also ACID, isolation level, locking, REPEATABLE READ, SERIALIZABLE, transaction.

总结:这个隔离接别的事务看不到其他事务未提交的数据。可以看到事务开始后别的事务已经提交的修改。

REPEATABLE READ 可重复读

The default isolation level(默认隔离级别) for InnoDB. It prevents any rows(阻止任何行) that are queried from being changed by other transactions, thus blocking non-repeatable reads but not phantom reads. It uses a moderately strict locking strategy so that all queries within a transaction see data from the same snapshot, that is, the data as it was at the time the transaction started.(事务开启的时候)

When a transaction with this isolation level performs UPDATE … WHERE, DELETE … WHERE, SELECT … FOR UPDATE, and LOCK IN SHARE MODE operations, other transactions might have to wait.

SELECT … FOR SHARE replaces SELECT … LOCK IN SHARE MODE in MySQL 8.0.1, but LOCK IN SHARE MODE remains available for backward compatibility.

可重复读:简单说事务开始的时候数据是什么样,结束的时候就是什么样。

SERIALIZABLE

The isolation level that uses the most conservative locking strategy, to prevent any other transactions from inserting or changing data that was read by this transaction, until it is finished. This way, the same query can be run over and over within a transaction, and be certain to retrieve the same set of results each time. Any attempt to change data that was committed by another transaction since the start of the current transaction, cause the current transaction to wait.

This is the default isolation level specified by the SQL standard. In practice, this degree of strictness is rarely needed, so the default isolation level for InnoDB is the next most strict, REPEATABLE READ.

See Also ACID, consistent read, isolation level, locking, REPEATABLE READ, transaction.

总结:隔离级别最高,事务开启后,其他事务的修改操作必须排队,可以防止一切问题,但是效率最低。

phantom 幻读

A row that appears in the result set of a query, but not in the result set of an earlier query. For example, if a query is run twice within a transaction, and in the meantime, another transaction commits after inserting a new row or updating a row so that it matches the WHERE clause of the query.

This occurrence is known as a phantom read. It is harder to guard against than a non-repeatable read, because locking all the rows from the first query result set does not prevent the changes that cause the phantom to appear.

Among different isolation levels, phantom reads are prevented by the serializable read level, and allowed by the repeatable read, consistent read, and read uncommitted levels.

See Also consistent read, isolation level, non-repeatable read, READ UNCOMMITTED, REPEATABLE READ, SERIALIZABLE, transaction.

MVCC

Acronym for “Multi Version Concurrency Control”. This technique lets InnoDB transactions with certain isolation levels perform consistent read operations; that is, to query rows that are being updated by other transactions, and see the values from before those updates occurred. This is a powerful technique to increase concurrency(增加并发), by allowing queries to proceed without waiting due to locks held by the other transactions.

This technique is not universal in the database world. Some other database products, and some other MySQL storage engines, do not support it.

See Also ACID, concurrency, consistent read, isolation level, lock, transaction.

consistent read

A read operation that uses snapshot information to present query results based on a point in time, regardless of changes performed by other transactions running at the same time. If queried data has been changed by another transaction, the original data is reconstructed based on the contents of the undo log. This technique avoids some of the locking issues that can reduce concurrency by forcing transactions to wait for other transactions to finish.

With REPEATABLE READ isolation level, the snapshot is based on the time when the first read operation is performed. With READ COMMITTED isolation level, the snapshot is reset to the time of each consistent read operation.

Consistent read is the default mode in which InnoDB processes SELECT statements in READ COMMITTED and REPEATABLE READ isolation levels. Because a consistent read does not set any locks on the tables it accesses, other sessions are free to modify those tables while a consistent read is being performed on the table.

For technical details about the applicable isolation levels, see Section 15.7.2.3, “Consistent Nonlocking Reads”.

See Also concurrency, isolation level, locking, READ COMMITTED, REPEATABLE READ, snapshot, transaction, undo log.

snapshot 快照

A representation of data at a particular time, which remains the same even as changes are committed by other transactions. Used by certain isolation levels to allow consistent reads.

See Also commit, consistent read, isolation level, transaction.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/66292.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

深入浅出InnoDB Locking

https://dev.mysql.com/doc/refman/8.0/en/innodb-locking.html 本节讨论的所有锁都是在 InnoDB 引擎下 MySQL 实现行锁定主要使用共享锁和排他锁。也就是常说的读写锁。 A shared (S) lock permits the transaction that holds the lock to read a row. An exclusive (X) l…

若依多租户集成浅析(基于数据源隔离)

背景 这边有个做 saas 化应用的需求,要求做到数据源级别隔离,选了 RuoyiCRM: 基于若依Vue平台搭建的多租户独立数据库CRM系统, 项目不断迭代中。欢迎提BUG交流~ (gitee.com) 这个项目做分析 先放一下码云上作者画的图,后面我把整…

股票量化怎样分析股票数据精准选股?

在日常的股票量化交易过程中,通常有不少的交易者会借助股票数据接口来分析股票数据,并且经过一番股票量化分析之后,做到精准选股也是很有可能的事情。那么,普通投资者进行股票量化怎样分析股票数据选好股呢? 首先来了…

springboot:集成Kaptcha实现图片验证码

文章目录springboot:集成Kaptcha实现图片验证码一、导入依赖系统配置文件二、生成验证码1、Kaptcha的配置2、自定义验证码文本生成器3、具体实现三、校验验证码1、controller接口2、自定义前端过滤器3、自定义验证码处理过滤器4、自定义BodyReaderFilter解决读取bod…

Redis——Jedis的使用

前言 接上文,上一篇文章分享了在Linux下安装redis,以及redis的一些命令的使用。本文要分享的内容是java使用代码连接操作redis。 一、连接redis 这里我们要用到Jedis,那么什么是Jedis 简单来说,Jedis就是Redis官方推荐的Java连接…

【元胞自动机】模拟电波在整个心脏中的传导和传播的时空动力学研究(Matlab代码实现)

👨‍🎓个人主页:研学社的博客 💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜…

(八)SpringCloud+Security+Oauth2--token增强个性化和格式化输出

一 token的个性化输出 我们知道token默认的输出格式是: {"access_token": "21bd6b0b-0c24-40d1-8928-93274aa1180f","token_type": "bearer","refresh_token": "2c38965b-d4ce-4151-b88d-e39f278ce1bb","e…

[思考进阶]02 如何进行认知升级?

除了要提升自己的技术能力,思维的学习和成长也非常非常重要,特推出此[思考进阶]系列,进行刻意练习,从而提升自己的认知。 最近在看东野的《无名之町》,这本书写于2021年,日本正值疫情,书中也有大…

这个项目获2022世界物联网博览会三新成果奖!

近日,2022世界物联网无锡峰会在无锡太湖国际博览中心召开。天翼物联科技有限公司副总经理赵建军代表中国电信出席会议。 大会颁发了“物联网新技术新产品新应用金奖成果奖”(简称“三新成果奖”),中国电信天翼物联“基于5G物联孪…

gRPC:以 C++为例

文章目录1、gRPC 环境搭建1.1、安装 cmake1.2、安装 gcc/gdb1.3、安装 gRPC1.4、protobuf 安装1.5、测试环境2.1、grpc 同步2.1、定义服务2.2、gRPC 服务端2.3、gRPC 客户端2.4、消息流3、gRPC stream3.1、服务端:RPC 实现3.2、客户端:RPC 调用3.3、流的…

刷爆力扣之子数组最大平均数 I

刷爆力扣之子数组最大平均数 I HELLO,各位看官大大好,我是阿呆 🙈🙈🙈 今天阿呆继续记录下力扣刷题过程,收录在专栏算法中 😜😜😜 该专栏按照不同类别标签进行刷题&…

Centos 8.2 本地部署 Jenkins

文章目录1. 简介2. 准备条件3. 安装依赖工具4. 配置 jenkins 源5. 安装 java 176. 安装 Jenkins7. 登陆8. 安装插件8.1 kubernets 插件8.2 git 插件8.3 docker 插件9. 创建 pipeline job9.1 加载本地 Jenkinsfile 构建9.2 git 构建10. 问题1. 简介 Jenkins 是一个 CI/CD 工具。…

Transformer是如何进军点云学习领域的?

点击进入—>3D视觉工坊学习交流群0.笔者个人体会:这个工作来自于牛津大学、香港大学、香港中文大学和Intel Labs,发表于ICCV2021。我们知道,Transformer在近两年来于各个领域内大放异彩。其最开始是自然语言处理领域的一个强有力的工具。后…

Unity 动画系统(Animation,Animator,Timeline)

文章目录1. Animation1.1 创建Animation1.2 Animation 属性2. Animator2.1 Animator 组件2.2 Animation 状态2.3 状态控制参数2.4 代码中控制状态3. 代码控制动画的播放/暂停/继续播放1. Animation 1.1 创建Animation 选中需要添加动画的物体,打开Animation面板 …

乡村科技杂志乡村科技杂志社乡村科技编辑部2022年第20期目录

三农资讯 科技特派员助力柘城县大豆玉米带状复合种植见成效 宋先锋;贾志远; 1《乡村科技》投稿:cnqikantg126.com 河南省科技特派员赴遂平县指导多花黑麦草防治 蒋洪杰;欧阳曦; 2 河南省肉牛产业科技特派员服务团到光山县开展技术培训服务 翟媛媛;朱燚波…

la3_系统调用(上)

1. 实验内容 理解操作系统接口;系统调用的实现: 应用程序 调用库函数 (API)API 将 系统调用号 放入 EAX 中, 然后通过中断调用 使系统进入内核态;内核中的中断处理函数 根据系统调用号, 调用对…

通过postgres_fdw实现跨库访问

瀚高数据库 目录 文档用途 详细信息 介绍Postgresql跨库访问中postgres_fdw的使用方法 详细信息 PostgreSQL 外部数据包装器,即 PostgreSQL Foreign Data Wrappers,是现实数据库使用场景中一个非常实用的功能,PostgreSQL 的 FDW 类似于 Ora…

2022年12月编程语言排行榜,数据来了!

2022年迎来了最后一个月,我们可以看到,在这一年中编程语言起起伏伏,有的语言始终炙手可热,而有的语言却逐渐“没落”… 日前,全球知名TIOBE编程语言社区发布了12月编程语言排行榜,有哪些新变化&#xff1f…

木聚糖-聚乙二醇-透明质酸,Hyaluronicacid-PEG-Xylan,透明质酸-PEG-木聚糖

木聚糖-聚乙二醇-透明质酸,Hyaluronicacid-PEG-Xylan,透明质酸-PEG-木聚糖 中文名称:木聚糖-透明质酸 英文名称:Xylan-Hyaluronicacid 别称:透明质酸修饰木聚糖,HA-木聚糖 存储条件:-20C,避光&#xff…

农产品商城毕业设计,农产品销售系统毕业设计,农产品电商毕业设计论文方案需求分析作品参考

项目背景和意义 目的:本课题主要目标是设计并能够实现一个基于web网页的多用户商城系统,整个网站项目使用了B/S架构,基于python的Django框架下开发;用户通过登录网站,查询商品,购买商品,下单&am…