当前位置:网站首页>第十一章 事务与锁
第十一章 事务与锁
2022-08-06 05:18:00 【ee .】
第十一章 事务与锁
1.理解事务的概念和4个基本特性
答:事务是用户定义的一个操作序列。这些操作要么都做,要么都不做,是一个不可分割的工作单元。
事务的四个特性:原子性,一致性,隔离性,持久性。
2.掌握事务的开启,提交和回滚
答:a.开启事务
start transaction;
b.事务的提交
使用:commit
如果不显示的开启一个事务,则每个sql语句都被当作一个事务执行提交操作,对于需 要执行多条sql语句才能完成的事务来说,必须要关闭MySQL的自动提交功能。
查看自动提交功能是否开启:show variables like 'autocommit';
关闭自动提交功能:set autocommit=0;
注意:autocommit是会话变量,只在当前命令窗口有效。当使用start transaction命令 是可以自动隐式地关闭自动提交功能,且不会修改autocommit变量的值。
c.事务的回滚
rollback;
保存点:savepoint 保存点名;
回滚到保存点:rollback to savepoint 保存点名;
3.掌握事务的4种隔离级别
答:事务的隔离级别:
级别一. 未提交读:read uncommitted (产生:脏读,不可重复读,幻读)
级别二. 提交读:read committed(产生:不可重复读,幻读)
级别三. 可重复读:repeatable read (产生:幻读)
级别四. 可串行化:serializable(易出现超时现象和锁竞争)
查看隔离级别:全局:select @@global.transaction_isolation;
当前会话:select @@session.transaction_isolation;
下一个事务:select @@transaction_isolation;(仅对当前用户的下一个事务有影响)
修改隔离级别:set transaction设置隔离级别(使用session为当前会话级别,global为全局隔 离级别)
set session transaction isolation level read committed;
set 语句设置隔离级别
set @@session.transaction_isolation='read-uncommitted';
4.了解锁机制,理解并掌握锁管理的过程
答:锁的颗粒度:指的是锁的作用范围。分为:表级锁(类型包括读锁,写锁),行级锁(类型包括排他锁,共享锁,意向锁),页面锁。
锁的管理:
1)表级锁:
上锁:lock tables tbl_name lock_type; eg:lock tables jsjbxxb read;
解锁:unlock tables;
2)行级锁(语句操作完毕,服务器会自动为其解锁)
上读锁:select 语句 for share 【nowait | skip locked】lock in share mode;
上写锁:select 语句 for update 【nowait | skip locked】
用事务来演示:
start transaction;
select *from jsjbxxb where jsxm='张三' for update;(上写锁,无法更新张三信息)
rollback;(回滚后可执行更新操作)
边栏推荐
- How to avoid the account risk of cross-border e-commerce risk?What is the solution?
- 从业务开发中学习和理解架构设计
- 【C#】试卷批改系统
- 2022 list of popular overseas social platforms abroad!
- Routing - the route
- aws篇7
- A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
- 互联网时代下服务器该如何进行主机加固
- Practical debugging tips
- C language advanced articles: custom types--bit segment, enumeration and union
猜你喜欢

【初学C必看!】涵盖 知 识 点 很多的扫雷游戏!

Temperature (acute/PH/electric/temperature/PH sensitive/double magnetic field sensitive hydrogel preparation

Develop a SQL editor with Monaco Editor

Jhipster介绍和使用

高并发下秒杀商品,你必须知道的9个细节

C基础:45道练习题汇总(初学者加油)

Practical debugging tips

【收藏向】LaTeX 符号大全

数学建模学习(78):多输入多输出回归预测模型(结合XGBoost实现)

【递归详解】C语言解决汉诺塔问题
随机推荐
redis的常用命令及使用特点
el-table style modification
A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
跨域请求处理之配置代理模式
调用openssl api函数C代码生成证书
ES6之面对对象 class类
2066:【例2.3】买图书
【ROS2组成原理1】编译和构造
当下企业源代码数据防泄密工作该如何进行
一文彻底解析数据库设计思路
百度智能云数字人凭什么领跑中国AI数字人?
ModuleNotFoundError: No module named ‘nonebot.adapters‘ 错误的可能解决方法
There are benefits at the end of the article | What adb commands have you mastered commonly used in App testing?
redis的集群模式
【三子棋实例】C语言实现三子棋游戏
贩卖使用了试用版数据库的软件,会构成侵权吗
1005:地球人口承载力估计
基于flask框架web代码pyinstaller打包
2位字母组合的全球国家或地区缩写
fegin feign.FeignException: status 404 reading StudenService#getAll()