当前位置:网站首页>Distributed lock database implementation
Distributed lock database implementation
2022-07-28 06:03:00 【Performer】
1:for update Principle
select Retrieved data , for update Added a lock , Others cannot modify this data , You can't lock this data anymore . Other threads can retrieve , But I'm using it for update It is impossible to lock these data , Because this lock , It has been locked by the previous thread . Others cannot lock it , During locking , No one else can modify these data . because update Update the data , Is to obtain the lock of these data
2: Code implementation
@Transactional(rollbackFor = Exception.class)
@RequestMapping("/database")
public String databaseLock () throws InterruptedException {
log.info(" How to get in ");
BusinessLock businessLock = businessLockMapper.getLock("demo");
if (ObjectUtils.isEmpty(businessLock)) {
log.info(" Distributed lock not found ");
}
log.info(" Enter the lock ");
Thread.sleep(6000);
log.info(" Method execution complete ");
return " Method execution complete ";
}mapper Inside sql
SELECT * FROM business_lock WHERE business_code = #{businessCode} FOR UPDATE
- Open two instances , The ports are 8080 8088
- The figure below 8080 application
- 58:16 second How to get in
- 58:16 second Enter the lock
- 58:22 second Method execution complete

- The figure below 8088 application
- 58:17 second How to get in
- 58:22 second Enter the lock
- 58:28 second Method execution complete

In the code, we let the application that gets the lock sleep 6 second ,8080 and 8088 Enter the method at about the same time ,
however 8080 Earlier, all got the lock , and 8088 Just block in the database , Wait for lock .
stay 16 second 8080 Obtain lock wait 6 second 22 Second release lock , At this time 8088 Just got the lock . It shows that locks are effective in many applications .
Tutorial source code +sql download
3: use native measuring for update
- First, automatically submit the database of the current page and turn it off
Why turn it off first ? After you retrieve it now or for update Lock up , After the lock is added, the transaction will be submitted automatically , The transaction is committed . The lock will automatically release . Other conversations , This data will also be retrieved
- The figure below : The default is to automatically commit transactions

- The figure below : At this time, auto commit transactions are not turned off . Both can execute locking pages . It doesn't play the role we want

- The figure below : Close the auto commit transaction of the two windows

- Retrieve again with for update Keywords sql . At this time, the lock is locked by the left window , The right window cannot be locked . At this time The left window executes commit Commit transaction operations , The right window immediately finds the data . It means that the lock is effective

- The left window executes for update , The right window is not used for update Instead, the query is executed directly Data can be queried

4: summary

边栏推荐
猜你喜欢

预告来袭:【豆冰冰】发.售,现.金.抽.奖等你来拿

Sales notice: on July 22, the "great heat" will be sold, and the [traditional national wind 24 solar terms] will be sold in summer.

【五】redis主从同步与Redis Sentinel(哨兵)

(php毕业设计)基于thinkphp5小区物业管理系统

1:开启慢查询日志 与 找到慢SQL

Related concepts and operations of DOM model

Mars number * word * Tibet * product * Pingtai defender plan details announced

Books - social animals

CMD and NPM basic commands

数据仓库工具superset安装(完全可用)
随机推荐
使用pycharm创建虚拟环境
Data warehouse tool superset installation (fully available)
疫情当下,线下文旅受困,看数字藏品能否解围?
1:为什么数据库要分库分表
JS promise implementation logic
【一】redis简介
(PHP graduation project) obtained based on PHP student homework submission management system
(php毕业设计)基于php校园网络报修管理系统获取
Community epidemic access management system based on PHP (PHP graduation design)
Single line function, aggregate function after class exercise
Flink CDC (Mysql为例)
Chapter IX sub query (key)
Books - poor Charlie's classic
1:开启慢查询日志 与 找到慢SQL
Books - smart investors
The combination of cultural tourism and digital collections has a significant effect, but how to support users' continuous purchasing power
MarsNFT :个人如何发行数字藏品?
MySQL trigger
【六】redis缓存策略
XShell突然间无法连接虚拟机