当前位置:网站首页>MySQL乐观锁
MySQL乐观锁
2022-07-25 15:43:00 【Leon_Jinhai_Sun】
乐观锁认为对同一数据的并发操作不会总发生,属于小概率事件,不用每次都对数据上锁,但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,也就是 不采用数据库自身的锁机制,而是通过 程序来实现 。在程序上,我们可以采用 版本号机制 或者 CAS 机制 实现。 乐观锁适用于多读的应用类型, 这样可以提高吞吐量 。在 Java 中 java.util.concurrent.atomic 包下的原子变量类就是使用了乐观锁的一种实现方式:CAS 实现的。
1. 乐观锁的版本号机制 : 在表中设计一个 版本字段 version ,第一次读的时候,会获取 version 字段的取值。然后对数据进行更新或删除操作时,会执行 UPDATE ... SET version=version+1 WHERE version=version 。此时如果已经有事务对这条数据进行了更改,修改就不会成功。
2. 乐观锁的时间戳机制 : 时间戳和版本号机制一样,也是在更新提交的时候,将当前数据的时间戳和更新之前取得的时间戳进行比较,如果两者一致则更新成功,否则就是版本冲突。你能看到乐观锁就是程序员自己控制数据并发操作的权限,基本是通过给数据行增加一个戳(版本号或者时间戳),从而证明当前拿到的数据是否最新。
边栏推荐
- How to realize page inclusion
- 谷歌博客:采用多重游戏决策Transformer训练通用智能体
- R语言偏相关性计算(Partial Correlation)、使用ggm包的pcor函数计算偏相关性(Partial Correlations)
- ML - Speech - traditional speech model
- MySQL—常用SQL语句整理总结
- IDEA—点击文件代码与目录自动同步对应
- CVPR 2022 | 网络中批处理归一化估计偏移的深入研究
- LeetCode - 707 设计链表 (设计)
- Matlab simulation of BPSK modulation system (1)
- Where is there a demo to set up the flex CDC to draw the number of MySQL?
猜你喜欢
![[server data recovery] data recovery cases of raid information loss caused by unexpected power failure of HP EVA server storage](/img/90/51d86111b918eb60761818110cdec4.jpg)
[server data recovery] data recovery cases of raid information loss caused by unexpected power failure of HP EVA server storage

SVD singular value decomposition derivation and application and signal recovery

CVPR 2022 | 网络中批处理归一化估计偏移的深入研究

Baseband simulation system experiment of 4pam in Gaussian channel and Rayleigh channel

IDEA—点击文件代码与目录自动同步对应

用GaussDB(for Redis)存画像,推荐业务轻松降本60%

不愧是阿里内部“千亿级并发系统架构设计笔记”面面俱到,太全了

Redis distributed lock, it's really impossible without it

基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现

Introduction to redis
随机推荐
C # fine sorting knowledge points 12 exception handling (recommended Collection)
Copy a word style template to another document
JVM—类加载器和双亲委派模型
Use cpolar to build a business website (how to buy a domain name)
Endnote add Chinese gbt7714 style how to quote documents in word
July 25th, 2022 Daily: Microsoft proposed CodeT: a new SOTA for code generation, with 20 points of performance improvement
CircleIndicator组件,使指示器风格更加多样化
「数字安全」警惕 NFT的七大骗局
LeetCode - 379 电话目录管理系统(设计)
Beyond Compare 4 实现class文件对比【最新】
Pytoch learning notes advanced_ CNN (using perception_module) implements MNIST dataset classification - (comments and results)
LeetCode - 622 设计循环队列 (设计)
How to solve cross domain problems
30行自己写并发工具类(Semaphore, CyclicBarrier, CountDownLatch)
Cf365-e - Mishka and divisors, number theory +dp
MySQL tutorial 65 data in MySQL operation table
LeetCode - 232 用栈实现队列 (设计 双栈实现队列)
Leetcode - 379 telephone directory management system (Design)
Sword finger offer | number of 1 in binary
How matlab produces random complex sequences