登录
首页 >  数据库 >  MySQL

共享锁,排他锁的现实理解

来源:SegmentFault

时间:2023-01-16 12:11:59 464浏览 收藏

对于一个数据库开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《共享锁,排他锁的现实理解》,主要介绍了MySQL,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

Think of a lockable object as a blackboard (lockable) in a class room containing a teacher (writer) and many students (readers).

While a teacher is writing something (exclusive lock) on the board:

Nobody can read it, because it's still being written, and she's blocking your view => If an object is exclusively locked, shared locks cannot be obtained.

Other teachers won't come up and start writing either, or the board becomes unreadable, and confuses students => If an object is exclusively locked, other exclusive locks cannot be obtained.

When the students are reading (shared locks) what is on the board:

They all can read what is on it, together => Multiple shared locks can co-exist.

The teacher waits for them to finish reading before she clears the board to write more => If one or more shared locks already exist, exclusive locks cannot be obtained.

LOCK IN SHARE MODE 适合用于两张表存在业务关系上的一致性要求时的操作场景。
SELECT ... LOCK IN SHARE MODE的应用场景适合于两张表存在关系时的写操作,拿MySQL官方文档的例子来说,假如存在两张有关系的表:PARENT和CHILD,使用普通的SELECT语句(快照读)来查询表PARENT并验证父行是否存在后再将子行插入CHILD表,这种方式安全吗?答案是否定的,因为其他会话可能会在你这个会话的SELECT和INSERT之间的某个时间点删除了父行,这个删除操作你是无法察觉到的。

为避免这种潜在的问题,我们使用“加共享锁”的方式执行SELECT

文中关于mysql的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《共享锁,排他锁的现实理解》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表