public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {
@Autowired
private ISeckillVoucherService iSeckillVoucherService;
@Autowired
private RedisIdWorker redisIdWorker;
@Override
@Transactional
public Result seckillVoucher(Long voucherId) {
//1.查询优惠券
SeckillVoucher voucher = iSeckillVoucherService.getById(voucherId);
//2.判断秒杀是否开始
if(voucher.getBeginTime().isAfter(LocalDateTime.now())){
return Result.fail("秒杀尚未开始!");
}
//3.判断秒杀是否结束
if(voucher.getEndTime().isBefore(LocalDateTime.now())){
return Result.fail("秒杀已经结束!");
}
//4.判断库存是否充足
if(voucher.getStock() < 1){
return Result.fail("库存不足!");
}
//5.扣减库存
boolean success = iSeckillVoucherService.update().setSql("stock = stock -1").eq("voucher_id",voucherId).update();
if(!success){
//扣减失败
return Result.fail("库存不足!");
}
//6.创建订单
VoucherOrder voucherOrder = new VoucherOrder();
//6.1 订单id
long orderId = redisIdWorker.nextId("order");
voucherOrder.setId(orderId);
//6.2 用户id
Long userId = UserHolder.getUser().getId();
voucherOrder.setUserId(userId);
//6.3 代金券id
voucherOrder.setVoucherId(voucherId);
//6.4 记录数据库
save(voucherOrder);
//7.返回订单id
return Result.ok(orderId);
}
}
public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {
@Autowired
private ISeckillVoucherService iSeckillVoucherService;
@Autowired
private RedisIdWorker redisIdWorker;
@Override
@Transactional
public Result seckillVoucher(Long voucherId) {
//1.查询优惠券
SeckillVoucher voucher = iSeckillVoucherService.getById(voucherId);
//2.判断秒杀是否开始
if(voucher.getBeginTime().isAfter(LocalDateTime.now())){
return Result.fail("秒杀尚未开始!");
}
//3.判断秒杀是否结束
if(voucher.getEndTime().isBefore(LocalDateTime.now())){
return Result.fail("秒杀已经结束!");
}
//4.判断库存是否充足
if(voucher.getStock() < 1){
return Result.fail("库存不足!");
}
//5.扣减库存
boolean success = iSeckillVoucherService.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("库存不足!");
}
//6.创建订单
VoucherOrder voucherOrder = new VoucherOrder();
//6.1 订单id
long orderId = redisIdWorker.nextId("order");
voucherOrder.setId(orderId);
//6.2 用户id
Long userId = UserHolder.getUser().getId();
voucherOrder.setUserId(userId);
//6.3 代金券id
voucherOrder.setVoucherId(voucherId);
//6.4 记录数据库
save(voucherOrder);
//7.返回订单id
return Result.ok(orderId);
}
}
通过synchronized来给每一个用户的id加锁
public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {
@Autowired
private ISeckillVoucherService iSeckillVoucherService;
@Autowired
private RedisIdWorker redisIdWorker;
@Override
public Result seckillVoucher(Long voucherId) {
//1.查询优惠券
SeckillVoucher voucher = iSeckillVoucherService.getById(voucherId);
//2.判断秒杀是否开始
if(voucher.getBeginTime().isAfter(LocalDateTime.now())){
return Result.fail("秒杀尚未开始!");
}
//3.判断秒杀是否结束
if(voucher.getEndTime().isBefore(LocalDateTime.now())){
return Result.fail("秒杀已经结束!");
}
//4.判断库存是否充足
if(voucher.getStock() < 1){
return Result.fail("库存不足!");
}
//7.返回订单id
Long userId = UserHolder.getUser().getId();
//对同一用户的id进行加锁,如果直接在createVoucherOrder这个方法里加锁,会导致先释放锁在提交事务,这样还是会有问题。
//事务的管理是通过代理对象来的,所以先创建一个代理对象来
synchronized (userId.toString().intern()){
//获取代理对象(事务)
IVoucherOrderService proxy = (IVoucherOrderService) AopContext.currentProxy();
return proxy.createVoucherOrder(voucherId);
}
}
@Transactional
public Result createVoucherOrder(Long voucherId){
Long userId = UserHolder.getUser().getId();
//5. 一人一单
//5.1 查询订单
Integer count = query().eq("user_id", userId).eq("voucher_id", voucherId).count();
if (count > 0) {
return Result.fail("用户已经购买过一次!");
}
//6.扣减库存
boolean success = iSeckillVoucherService.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("库存不足!");
}
//7.创建订单
VoucherOrder voucherOrder = new VoucherOrder();
//6.1 订单id
long orderId = redisIdWorker.nextId("order");
voucherOrder.setId(orderId);
//6.2 用户id
voucherOrder.setUserId(userId);
//6.3 代金券id
voucherOrder.setVoucherId(voucherId);
//6.4 记录数据库
save(voucherOrder);
return Result.ok(voucherId);
}
}
核心代码实现:利用设置键的同时设置它的过期时间(这样时间一到这个键也就失效了),中间执行业务,最后再释放锁。以此可以实现防止同个用户下多单的情况。
Long userId = UserHolder.getUser().getId();
//创建锁对象
SimpleRedisLock lock = new SimpleRedisLock("order:" + userId, stringRedisTemplate);
//获取锁,同一个用户一旦获取了锁之后,他就不能在同一时间在继续访问了,等到锁被释放,此时用户下单记录已经被记住数据库了,实现了”一人一单“的功能
boolean isLock = lock.tryLock(500);
//判断是否获锁成功
if(!isLock){
//获取锁失败
return Result.fail("不允许重复下单");
}
try{
//获取代理对象(事务)
IVoucherOrderService proxy = (IVoucherOrderService) AopContext.currentProxy();
return proxy.createVoucherOrder(voucherId);
}finally {
//释放锁
lock.unlock();
}
完整代码:
@Override
public Result seckillVoucher(Long voucherId) {
//1.查询优惠券
SeckillVoucher voucher = iSeckillVoucherService.getById(voucherId);
//2.判断秒杀是否开始
if(voucher.getBeginTime().isAfter(LocalDateTime.now())){
return Result.fail("秒杀尚未开始!");
}
//3.判断秒杀是否结束
if(voucher.getEndTime().isBefore(LocalDateTime.now())){
return Result.fail("秒杀已经结束!");
}
//4.判断库存是否充足
if(voucher.getStock() < 1){
return Result.fail("库存不足!");
}
Long userId = UserHolder.getUser().getId();
//创建锁对象
SimpleRedisLock lock = new SimpleRedisLock("order:" + userId, stringRedisTemplate);
//获取锁,同一个用户一旦获取了锁之后,他就不能在同一时间在继续访问了,等到锁被释放,此时用户下单记录已经被记住数据库了,实现了”一人一单“的功能
boolean isLock = lock.tryLock(500);
//判断是否获锁成功
if(!isLock){
//获取锁失败
return Result.fail("不允许重复下单");
}
try{
//获取代理对象(事务)
IVoucherOrderService proxy = (IVoucherOrderService) AopContext.currentProxy();
return proxy.createVoucherOrder(voucherId);
}finally {
//释放锁
lock.unlock();
}
}
解决方法:给每一个线程一个唯一的标识号,当删锁的时候取出当前线程的标识号,并与锁的标识号比较,一致才可以删。
public class SimpleRedisLock implements ILock {
private StringRedisTemplate stringRedisTemplate;
private String name; // 业务名称
public SimpleRedisLock(String name,StringRedisTemplate stringRedisTemplate){
this.name = name;
this.stringRedisTemplate = stringRedisTemplate;
}
private static final String KEY_PREFIX = "lock:";
private static final String ID_PREFIX = UUID.randomUUID().toString(true) + "-";
/**
* 获取锁
* @param timeoutSec 锁持有的超时时间,过期后自动释放
* @return
*/
@Override
public boolean tryLock(long timeoutSec) {
// 获取线程标识
String threadId = ID_PREFIX + Thread.currentThread().getId();
// 获取锁
Boolean success = stringRedisTemplate.opsForValue().setIfAbsent(KEY_PREFIX + name, threadId, timeoutSec, TimeUnit.SECONDS);
return Boolean.TRUE.equals(success);
}
@Override
public void unlock() {
// 获取线程标识
String threadId = ID_PREFIX + Thread.currentThread().getId();
// 获取锁中的标示
String id = stringRedisTemplate.opsForValue().get(KEY_PREFIX + name);
// 判断标识是否一致
if(threadId.equals(id)){
//释放锁
stringRedisTemplate.delete(KEY_PREFIX + name);
}
}
}
1.原来没有原子性原因:if判断和删除锁操作是没有原子性的,在判断和删除之间可能会发生事务阻塞,导致误删锁。
if(threadId.equals(id)){
//释放锁
stringRedisTemplate.delete(KEY_PREFIX + name);
}
2.解决方法:利用lua脚本来实现原子性
lua脚本:
-- 比较线程标识与锁中的标识是否一致
if(redis.call('get',KEYS[1]) == ARGV[1]) then
-- 释放锁 del key
return redis.call('del',KEYS[1])
end
return 0
java代码实现:
@Override
public void unlock() {
// 调用lua脚本
stringRedisTemplate.execute(
unlock_script,
Collections.singletonList(KEY_PREFIX + name), // 键
ID_PREFIX + Thread.currentThread().getId()); // 线程标识
}