当前位置:网站首页>在企业级开发过程中我发现有位同事用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
边栏推荐
- JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
- Unimportant tokens can be stopped in advance! NVIDIA proposes an efficient visual transformer network a-vit with adaptive token to improve the throughput of the model
- Adding, deleting, modifying and checking in low build code
- Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
- Domain Driven Design and coding
- December 6, 2019 what happens after the browser enters a URL
- Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
- U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency
- What are the advantages of VR panoramic production? Why is it favored?
- Interesting checkbox counters
猜你喜欢

iNFTnews | 国内NFT发展仅限于数字藏品吗?

im即时通讯开发应用保活之进程防杀

不重要的token可以提前停止计算!英伟达提出自适应token的高效视觉Transformer网络A-ViT,提高模型的吞吐量!...

Creative SVG ring clock JS effect

What are the advantages of VR panoramic production? Why is it favored?

传输层 以字节为单位的滑动窗口技术

U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency

Collective example

What exactly is Nacos

What is the difference between one way and two way ANOVA analysis, and how to use SPSS or prism for statistical analysis
随机推荐
[interview question] the difference between instancof and getclass()
What are the advantages of VR panoramic production? Why is it favored?
Some examples of MgO operating database in go
linux 系统redis常用命令
Svg+js keyboard control path
Fast pace? high pressure? VR panoramic Inn brings you a comfortable life
Current situation and development prospect forecast report of global and Chinese tetrahydrofurfuryl alcohol acetate industry from 2022 to 2028
Wx applet jump page
Tape SVG animation JS effect
wx小程序跳转页面
5-minute NLP: summary of 3 pre training libraries for rapid realization of NER
JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
The drawableleft of the custom textview in kotlin is displayed in the center together with the text
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
MySQL semi sync replication
Analysis report on development trend and investment forecast of global and Chinese D-leucine industry from 2022 to 2028
U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency
The new employee of the Department after 00 is really a champion. He has worked for less than two years. The starting salary of 18K is close to me when he changes to our company
Use of JMeter