当前位置:网站首页>【Redis实现秒杀业务④】一人一单,不可重复购买
【Redis实现秒杀业务④】一人一单,不可重复购买
2022-06-24 20:01:00 【步尔斯特】
对于秒杀水果,一个用户只能秒杀一种水果的一单。
业务流程

并发问题
正常
并发问题
加锁保证线程安全
判断订单是否存在,为了保证线程安全问题,我们可以加锁,synchronized。

如果加在方法上,是对this加锁,也就是说,所有的用户进来都要加锁,这样不符合我们一人一单的预期。
我们以用户ID加锁,减少锁的范围,提升性能。
userId.toString().intern()保证唯一【返回字符串对象的规范表示】
@Transactional
public Result createVoucherOrder(Long voucherId) {
// 一人一单【前文配置的过滤器中,如果登录的用户,会在ThreadLocal中添加用户信息】
Long userId = UserHolder.getUser().getId();
// 对执行代码块加锁,intern()方法避免资源浪费
synchronized (userId.toString().intern()) {
// 查询订单
int count = query().eq("user_id", userId).eq("voucher_id", voucherId).count();
// 判断是否存在
if (count > 0) {
// 用户已经购买过了
return Result.fail("用户已经购买过一次!");
}
// 扣减库存
boolean success = seckillVoucherService.update()
.setSql("stock = stock - 1") // set stock = stock - 1
.eq("voucher_id", voucherId).gt("stock", 0) // where id = ? and stock > 0
.update();
if (!success) {
// 扣减失败
return Result.fail("库存不足!");
}
集群问题
如果在集群的环境下,会有多个JVM存在,会存在多个锁监视器,这样加锁就会完全失效,还是会产生线程安全问题,所以,下一篇文章我们使用redis实现分布式锁来保证线程安全。

边栏推荐
- [proteus simulation] example of using timer 0 as a 16 bit counter
- C程序设计专题 15-16年期末考试习题解答(上)
- D omit parameter name
- How can I persuade leaders to use DDD to construct the liver project?
- Human body transformation vs digital Avatar
- Analysis report on development trend and investment forecast of global and Chinese D-leucine industry from 2022 to 2028
- VNC viewer remote connection raspberry pie without display
- ServerSocket and socket connection
- OTT营销之风正盛,商家到底该怎么投?
- Usage of assert
猜你喜欢

Ott marketing is booming. How should businesses invest?

Color gradient gradient color collection
Is it so difficult to calculate the REM size of the web page according to the design draft?

Svg+js keyboard control path
WordPress add photo album function [advanced custom fields Pro custom fields plug-in series tutorial]

On the difficulty of developing large im instant messaging system

Encryption and encoding resolution

Wallpaper applet wechat applet

Why are life science enterprises on the cloud in succession?
Fuxin Kunpeng joins in, and dragon lizard community welcomes a new partner in format document technical service
随机推荐
Svg line animation background JS effect
Qiniu cloud uploads video to get the first frame of video
Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
Dry and wet contacts
Some examples of MgO operating database in go
Solution to network access packet loss of Tencent cloud international ECS
软件测试与游戏测试文章合集录
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
Apk slimming compression experience
The picture of wechat official account can not be displayed normally
Design scheme of authority management of fusion model
C程序设计专题 18-19年期末考试习题解答(下)
离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解
Applet opening traffic master
Basic summary of MySQL database knowledge
Time unified system
C program design topic 18-19 final exam exercise solutions (Part 2)
OTT营销之风正盛,商家到底该怎么投?
Why are life science enterprises on the cloud in succession?
Wallpaper applet wechat applet