zoukankan      html  css  js  c++  java
  • 基于数据库悲观锁的分布式锁

    一、基于数据库悲观锁的分布式锁 

     代码

       

    package com.example.distributelock.controller;
    
    import com.example.distributelock.dao.DistributeLockMapper;
    import com.example.distributelock.model.DistributeLock;
    import com.example.distributelock.model.DistributeLockExample;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    @RestController
    @Slf4j
    public class DemoController {
        @Resource
        private DistributeLockMapper distributeLockMapper;
    
        @RequestMapping("singleLock")
        @Transactional(rollbackFor = Exception.class)
        public String singleLock() throws Exception {
            log.info("我进入了方法!");
            DistributeLock distributeLock = distributeLockMapper.selectDistributeLock("demo");
            if (distributeLock==null) throw new Exception("分布式锁找不到");
            log.info("我进入了锁!");
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "我已经执行完成!";
        }
    }
    Controller
  • 相关阅读:
    BCP 命令
    模板复习【updating】
    bzoj3716/4251 [PA2014]Muzeum
    bzoj4318 OSU!
    uoj308 【UNR #2】UOJ拯救计划
    bzoj4695 最假女选手
    省队集训 Day7 选点游戏
    hdu5828 Rikka with Sequence
    bzoj2482 [Spoj1557] Can you answer these queries II
    省队集训 Day6 序列
  • 原文地址:https://www.cnblogs.com/callbin/p/14580274.html
Copyright © 2011-2022 走看看