学技术学英文:代码中的锁:悲观锁和乐观锁

本文导读:

1. 举例说明加锁的场景:

多线程并发情况下有资源竞争的时候,如果不加锁,会出现数据错误,举例说明:

业务需求:账户余额>取款金额,才能取钱。 

时间线

两人共有账户

Alice取钱

Bob取钱

00:01

余额50

00:02

余额50

查询账户余额50

00:03

余额50

线程中断(cpu暂停执行)

查询账户余额50

00:04

余额20

线程中断(cpu暂停执行)

取30, 50>30, 可以取,执行sql: amount=amount-30

00:05

余额成了-20

取40, 刚才查询的余额为50,所以可以正常取,执行sql:amount=amount-40

这个例子中, 减余额是用sql减的(实际上是数据库服务器执行的减操作),数据库对某一条记录写操作的时候,都会加行锁,防止并发,所以执行sql update实际上是串行的,所以最终金额是-20,数据是对的, 就是不满足业务逻辑:账号余额小于取款金额不能取钱。

这个例子稍作调整,减金额,直接在自己的业务代码里先计算好,然后amount作为参数如传到sql里面直接update,那么最终金额就变成了 10 ,alice 的update会覆盖Bob的update,产生更严重的错误。

这个例子是以数据库为基础的,这样更直观,我们自己的代码也一样,自己设置的全局变量能被多个线程并发访问的时候,也需要做类似的考虑,但一般业务系统都会部署多个实例,所以业务系统中最常用的就是利用其他中间件实现的全局锁。

2. 加锁的两种方式:

悲观锁:提前阻止冲突,就是提前上锁(排他锁、写锁),上锁后独自一个你操作,操作完了其他人再操作,这样就避免并发数据错误。加锁开销高,代价大,适和并发冲突占比高的情况。

乐观锁:容许冲突,先不加锁,真正操作的时候能够检测到冲突,如果检测到了并发冲突,那么就回退重试,检测冲突一般通过版本号实现。适合并发冲突少的情况,只有少量的请求需要回退重试,那么性能就高。

When dealing with conflicts, you have two options:

  • You can try to avoid the conflict, and that's what Pessimistic Locking does.
  • Or, you could allow the conflict to occur, but you need to detect it upon committing your transactions, and that's what Optimistic Locking does.

Now, let's consider the following Lost Update anomaly:

The Lost Update anomaly can happen in the Read Committed isolation level.

In the diagram above we can see that Alice believes she can withdraw 40 from her account but does not realize that Bob has just changed the account balance, and now there are only 20 left in this account.

Pessimistic Locking

Pessimistic locking achieves this goal by taking a shared or read lock on the account so Bob is prevented from changing the account.

In the diagram above, both Alice and Bob will acquire a read lock on the account table row that both users have read. The database acquires these locks on SQL Server when using Repeatable Read or Serializable.

Because both Alice and Bob have read the account with the PK value of 1, neither of them can change it until one user releases the read lock. This is because a write operation requires a write/exclusive lock acquisition, and shared/read locks prevent write/exclusive locks.

Only after Alice has committed her transaction and the read lock was released on the account row, Bob UPDATE will resume and apply the change. Until Alice releases the read lock, Bob's UPDATE blocks.

Optimistic Locking

Optimistic Locking allows the conflict to occur but detects it upon applying Alice's UPDATE as the version has changed.

This time, we have an additional version column. The version column is incremented every time an UPDATE or DELETE is executed, and it is also used in the WHERE clause of the UPDATE and DELETE statements. For this to work, we need to issue the SELECT and read the current version prior to executing the UPDATE or DELETE, as otherwise, we would not know what version value to pass to the WHERE clause or to increment.

Application-level transactions

Relational database systems have emerged in the late 70's early 80's when a client would, typically, connect to a mainframe via a terminal. That's why we still see database systems define terms such as SESSION setting.

Nowadays, over the Internet, we no longer execute reads and writes in the context of the same database transaction, and ACID is no longer sufficient.

For instance, consider the following use case:

Without optimistic locking, there is no way this Lost Update would have been caught even if the database transactions used Serializable. This is because reads and writes are executed in separate HTTP requests, hence on different database transactions.

So, optimistic locking can help you prevent Lost Updates even when using application-level transactions that incorporate the user-think time as well.

Conclusion

Optimistic locking is a very useful technique, and it works just fine even when using less-strict isolation levels, like Read Committed, or when reads and writes are executed in subsequent database transactions.

The downside of optimistic locking is that a rollback will be triggered by the data access framework upon catching an OptimisticLockException, therefore losing all the work we've done previously by the currently executing transaction.

The more contention, the more conflicts, and the greater the chance of aborting transactions. Rollbacks can be costly for the database system as it needs to revert all current pending changes which might involve both table rows and index records.

For this reason, pessimistic locking might be more suitable when conflicts happen frequently, as it reduces the chance of rolling back transactions.

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

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

相关文章

django中cookie与session的使用

一、cookie cookie由服务器生成 ,存储在浏览器中的键值对数据,具有不安全性,对应敏感数据应该加密储存在服务端每个域名的cookie相互独立浏览器访问域名为A的url地址,会把A域名下的cookie一起传递到服务器cookie可以设置过期时间 django中设…

Qt编译MySQL数据库驱动

目录 Qt编译MySQL数据库驱动 测试程序 Qt编译MySQL数据库驱动 (1)先找到MySQL安装路径以及Qt安装路径 C:\Program Files\MySQL\MySQL Server 8.0 D:\qt\5.12.12 (2)在D:\qt\5.12.12\Src\qtbase\src\plugins\sqldrivers\mysql下…

CTFHUB-web(SSRF)

内网访问 点击进入环境,输入 http://127.0.0.1/flag.php 伪协议读取文件 /?urlfile:///var/www/html/flag.php 右击查看页面源代码 端口扫描 1.根据题目提示我们知道端口号在8000-9000之间,使用bp抓包并进行爆破 POST请求 点击环境,访问flag.php 查看页…

游戏引擎学习第43天

仓库 https://gitee.com/mrxiao_com/2d_game 介绍运动方程 今天我们将更进一步,探索运动方程,了解真实世界中的物理,并调整它们,以创建一种让玩家感觉愉悦的控制体验。这并不是在做一个完美的物理模拟,而是找到最有趣…

微服务中间件~nacos安全配置(含参考案例)

Nacos 是阿里巴巴开源的一个动态服务发现、配置管理和服务管理平台。为了确保 Nacos 的安全性,可以从以下几个方面入手: 1、启用身份验证: 在 application.properties 文件中启用身份验证功能:properties nacos.core.auth.enable…

【Leetcode 每日一题 - 扩展】45. 跳跃游戏 II

问题背景 给定一个长度为 n n n 的 0 0 0 索引 整数数组 n u m s nums nums。初始位置为 n u m s [ 0 ] nums[0] nums[0]。 每个元素 n u m s [ i ] nums[i] nums[i] 表示从索引 i i i 向前跳转的最大长度。换句话说,如果你在 n u m s [ i ] nums[i] nums[i…

UIP协议栈 TCP通信客户端 服务端,UDP单播 广播通信 example

文章目录 1. TCP通信 客户端(关键配置)2. TCP 服务端配置3. UDP 点播通信4. UDP 广播通信5. UIP_UDP_APPCALL 里边的处理example6. TCP数据处理 ,UIP_APPCALL调用的函数 UIP_APPCALL TCP的数据都在这个宏定义的函数里进行数据处理的 UDP 数据…

uniapp跨端适配—条件编译

在uniapp中,跨端适配是通过条件编译实现的。条件编译允许开发者根据不同的平台(如iOS、Android、微信小程序、百度小程序等)编写不同的代码。这样可以确保每个平台上的应用都能得到最优的性能和用户体验。 以下是uniapp中条件编译的基本语法…

YashanDB共享集群产品能力观测:细节足见功底

本文基于前泽塔数科研发总监-王若楠2024年11月在“2024年国产数据库创新生态大会”-“根”技术专场的演讲整理形成,主要对崖山共享集群YAC的架构、功能、高可用性、性能四大方面进行全面测试,并分享了测试环境和测试结论。 年初,基于某些商业…

VMware ubuntu16.04怎么设置静态IP联网

1.将VMware桥接到当前电脑使用的网络上面; 2.点击网络符号,编辑连接; 3.双击有线连接1; 4.选择IPv4设置,将地址,子网掩码,网关,DNS服务器设置好,保存; 5.在终…

用github镜像加速, --recursive还是去github站怎么处理?

小伙伴们大多碰到过github抽风的情况,时通时断,时快时慢,非常考验心情。 以前碰到连不上的时候,我大多就是在gitee和gitcode网站找一下镜像,找到后直接git clone 新地址即可。但是碰到 --recursive的时候就不行了&…

ElasticSearch的自动补全功能(拼音分词器、自定义分词器、DSL实现自动补全查询、RestAPI实现自动补全查询)

文章目录 1. 什么是自动补全2. 拼音分词器2.1 初识拼音分词器2.2 下载拼音分词器2.3 安装拼音分词器2.4 测试拼音分词器 3. 自定义分词器3.1 拼音分词器存在的问题3.2 分词器(analyzer)的组成3.3 如何自定义分词器3.4 拼音分词器的可选参数3.5 配置自定义…

Windows环境 (Ubuntu 24.04.1 LTS ) 国内镜像,用apt-get命令安装RabbitMQ,java代码样例

一、环境 Windows11 WSL(Ubuntu 24.04.1) 二、思路 1 用Windows中的Ubuntu安装RabbitMQ,贴近Linux的线上环境; 2 RabbitMQ用erlang语言编写的,先安装erlang的运行环境; 2 用Linux的apt-get命令安装,解决软件依赖…

医学预测模型的网页应用必要模块设计(重制版)

医学预测模型的网页应用必要模块设计(重制版) 刘岳鹏 摘要: 网页应用(Web APP)承载医学临床预测模型并在临床实践中实现与用户的互动,必要的功能模块设计将有助于Web APP更好地在临床实践中发挥其功能。在此…

【他山之石】Leading-Trim: The Future of Digital Typesetting:数字排版的未来 —— Leading-Trim

文章目录 【他山之石】Leading-Trim: The Future of Digital Typesetting:数字排版的未来 —— Leading-TrimHow an emerging CSS standard can fix old problems and raise the bar for web apps1. The problem with text boxes today2. How we got here: a histor…

[java] 简单的熔断器scala语言案例

failureRateInterval时间内如果addEx(错误)达到 maxFailuresPerInterval 次数,则fused方法返回true,表示触发熔断,进入冷却期coolingInterval,冷却期内fused方法返回true,冷却期过后进入下一个错误统计周期。 scala语言完成 imp…

【k8s集群应用】Kubernetes 容器编排系统

文章目录 Kubernetes 容器编排系统背景与发展Kubernetes 基本概念Kubernetes 集群架构与组件Kubernetes 核心组件Master 组件配置存储中心Node 组件 Kubernetes核心概念1. Pod2. Pod控制器3. Label与Label选择器4. Service5. Ingress6. Volume7. Name与Namespace K8S创建Pod资源…

梳理你的思路(从OOP到架构设计)_简介EIT造形

目录 1、 复习<基类 /子类>的扩充(extends)结构 典型的<基类/子类>代码结构 <基类/子类>代码结构的变形 2、 从<基类/子类>结构到EIT造形 3、 EIT造形的基本形与变形 1、 复习<基类 /子类>的扩充(extends)结构 典型的<基类/子类>代码…

CSS系列(11)-- 滤镜与混合模式详解

前端技术探索系列&#xff1a;CSS 滤镜与混合模式详解 &#x1f3a8; 致读者&#xff1a;探索视觉效果的艺术 &#x1f44b; 前端开发者们&#xff0c; 今天我们将深入探讨 CSS 滤镜与混合模式&#xff0c;学习如何创建独特的视觉效果。 滤镜效果详解 &#x1f680; 基础滤…

[创业之路-196]:华为成功经验的总结与教训简单总结

目录 前言&#xff1a; 成功经验 教训归纳 前言&#xff1a; 华为作为世界领先的通信设备制造商&#xff0c;其成功经验与教训值得深入探讨。 以下是对华为成功经验的总结与教训的归纳&#xff1a; 成功经验 战略定位明确&#xff1a; 华为始终坚持“死死抓住核心技术”…