当前位置:网站首页>在企业级开发过程中我发现有位同事用select * from where 条件 for update
在企业级开发过程中我发现有位同事用select * from where 条件 for update
2022-06-24 19:50:00 【SteveCode.】
活动地址:毕业季·进击的技术er
职场 内卷人
在企业级开发过程中我发现有位同事用select * from where 条件 for update
好吧! 今天我就学习了一手 顺便发个帖子。嘿嘿
本人借鉴的文章
面试官问:select…for update会锁表还是锁行?
开始巴!嘿嘿
看一段sql,感觉一下是不是很懵逼 可能也会有大佬
BEGIN;
SELECT * FROM a WHERE `key`=1 FOR UPDATE;
UPDATE a SET `name`='songxy9991' WHERE `key` =1;
COMMIT;
SELECT @@autocommit;
SET @@autocommit =0;
SELECT * FROM a WHERE `key`=1 FOR UPDATE;
COMMIT;
UPDATE a SET `name`='songxy12' WHERE `key` =1;
我们一个一个的说明
- 讲到这里 我们就会说到 事务这个 神圣而又伟大。要么成功要么都不成功。原则性、一致性、隔离性、持久性。等等巴,还有事务的隔离级别,还有spring 中的事务的传播特性 都是面试常考点
BEGIN
就是开启事务
COMMIT;
提交事务
SELECT @@autocommit;
查看事务是否静止自动提交事务
autocommit 状态值说明
- AUTOCOMMIT=0 禁止自动提交
- AUTOCOMMIT=1 开启自动提交
修改状态: - SET AUTOCOMMIT=0 禁止自动提交
- SET AUTOCOMMIT=1 开启自动提交
MySQL数据库默认会是 自动开启事务的

这边我们就 不让他自动提交事务 给他先禁止掉
SET @@autocommit =0;
SELECT @@autocommit;

真功夫开始了 哈
- 建表语句
DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
`key` int(0) NOT NULL AUTO_INCREMENT,
`value` char(225) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`key`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of a
-- ----------------------------
INSERT INTO `a` VALUES (1, 'ssss', 'songxy12');
INSERT INTO `a` VALUES (2, 'yy', 'songxy');
INSERT INTO `a` VALUES (3, 'sxy', 'songxy');
INSERT INTO `a` VALUES (4, '1111', 'songxy');
INSERT INTO `a` VALUES (5, 'qqq', 'songxy');
SET FOREIGN_KEY_CHECKS = 1;
先看一下我之前表数据

先通过主键查
SELECT * FROM a WHERE `key`=1 FOR UPDATE;
执行这个sql 我可没有 COMMIT;哈

在执行
UPDATE a SET `name`='songxy999' WHERE `key` =1;
去查表数据

- 没有发生改变
执行提交事务的语句
COMMIT;

- 到底锁的行还是 表 我来试试

- 报错了;error
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OPLNv5n5-1655994728162)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4af5d417b96646b0a4a1430460b7d594~tplv-k3u1fbpfcp-watermark.image?)]
看图

由此可见是锁行
活动地址:毕业季·进击的技术er
边栏推荐
- C program design topic 15-16 final exam exercise solutions (Part 1)
- A small program written this week
- Eye gaze estimation using webcam
- [proteus simulation] example of using timer 0 as a 16 bit counter
- How to use promise Race() and promise any() ?
- C WinForm maximizes occlusion of the taskbar and full screen display
- 部门新来的00后真是卷王,工作没两年,跳槽到我们公司起薪18K都快接近我了
- Hyperledger Fabric 2. X dynamic update smart contract
- How to quickly open traffic master for wechat applet
- Usage of ViewModel and livedata in jetpack
猜你喜欢

颜色渐变梯度颜色集合

Color gradient gradient color collection

无需显示屏的VNC Viewer远程连接树莓派
How to use promise Race() and promise any() ?

离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解

在滴滴和字节跳动干了 5年软件测试,太真实…

What are the advantages of VR panoramic production? Why is it favored?
![[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching](/img/bd/b176e93ee6fa2125f021bcad3025c2.png)
[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching

Go crawler framework -colly actual combat (II) -- Douban top250 crawling

Do280openshift access control -- encryption and configmap
随机推荐
C程序设计专题 15-16年期末考试习题解答(上)
Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source
【面试题】什么是事务,什么是脏读、不可重复读、幻读,以及MySQL的几种事务隔离级别的应对方法
More pictures | explain the Nacos parameters in detail!
信号完整性(SI)电源完整性(PI)学习笔记(一)信号完整性分析概论
Hibernate learning 3 - custom SQL
iNFTnews | 国内NFT发展仅限于数字藏品吗?
Tutorial details | how to edit and set the navigation function in the coolman system?
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
Paint rounded rectangle
Do280openshift access control -- encryption and configmap
Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...
UE4 WebBrowser图表不能显示问题
Report on operation mode and future development trend of global and Chinese propenyl isovalerate industry from 2022 to 2028
D manual destruction may violate memory security
Binder mechanism and Aidl communication example
【图数据库性能和场景测试利器LDBC SNB】系列一:数据生成器简介 & 应用于GES服务
软件测试与游戏测试文章合集录
C程序设计专题 18-19年期末考试习题解答(下)
JMeter socket connection sends data