• 论JAVA实现MYSQL 行级锁(分布式锁实现方案一)


     @Override
        @Transactional
        public String getCustomerId() {
    //        return String.valueOf(getSequenceId(SEQ_CUSTOMER_ITEM_CODE.seqName));
            String type=SEQ_CUSTOMER_ITEM_CODE.seqName;
    
            if (!sequenceValueMap.containsKey(type)) {
                SequenceDO sequenceDO = sequenceDOMapper.getBySeqNameForUpdate(type);
                LOGGER.info(Thread.currentThread().getName()+"===="+sequenceDO.getCurrentValue());
                sequenceValueMap.put(type, sequenceDO);
                //更新值
                int value = sequenceDO.getCurrentValue();
                sequenceDOMapper.updateSequenceValue(type, sequenceDO.getCurrentValue() + sequenceDO.getStep());
                sequenceDO.setCurrentValue(value + 1);
                LOGGER.info(Thread.currentThread().getName()+"----------->CurrentSequenceType:[{}],value:[{}]", type, sequenceDO.getCurrentValue());
    
                return sequenceDO.getCurrentValue()+"";
            } else {
                SequenceDO sequenceDO = sequenceValueMap.get(type);
                int value = sequenceDO.getCurrentValue() + 1;
                if (value % sequenceDO.getStep() == 0) {
                    //更新值
                    SequenceDO temp = sequenceDOMapper.getBySeqNameForUpdate(type);
                    LOGGER.info(Thread.currentThread().getName()+"----------->CurrentSequenceType:[{}],value:[{}]", type, temp.getCurrentValue());
                    sequenceDOMapper.updateSequenceValue(type, temp.getCurrentValue() + temp.getStep());
                    sequenceValueMap.put(type, temp);
                } else {
                    sequenceDO.setCurrentValue(value);
                }
                return value+"";
            }
        }
    

      使用行级锁 一定要注意 加上事务控制   MYSQL 行级锁才会起效,才会以排他的形式  访问

    -12-26 20:03:20.562  INFO  39993---[Thread-22]   c.s.m.s.i.SequenceServiceImpl - Thread-22====100198439
    2019-12-26 20:03:20.572  INFO  39993---[Thread-22]   c.s.m.s.i.SequenceServiceImpl - Thread-22----------->CurrentSequenceType:[sequenceCustomerInfo],value:[100198440]
    2019-12-26 20:03:20.577  INFO  39993---[ZkClient-EventThread-23-127.0.0.1:2181]   o.I.z.ZkEventThread - Terminate ZkClient event thread.
    2019-12-26 20:03:20.580  INFO  39993---[Thread-23]   c.s.m.s.i.SequenceServiceImpl - Thread-23====100198440
    2019-12-26 20:03:20.581  INFO  39993---[DubboShutdownHook]   o.a.z.ZooKeeper - Session: 0x100048539830052 closed
    2019-12-26 20:03:20.581  INFO  39993---[main-EventThread]   o.a.z.ClientCnxn - EventThread shut down
    100198440
    2019-12-26 20:03:20.589  INFO  39993---[Thread-23]   c.s.m.s.i.SequenceServiceImpl - Thread-23----------->CurrentSequenceType:[sequenceCustomerInfo],value:[100198441]
  • 相关阅读:
    Intellij IDEA使用姿势
    款阿里开源的 Java 诊断工具Arthas
    Spring Boot Runner启动器
    Spring Boot 2.x 启动全过程源码分析
    Spring Boot自动配置原理
    vue包部署在tomcat上,解决资源路径问题
    输入回车 回显换行
    session和cookie
    WebStorage——SessionStorage、LocalStorage与cookie
    HTML5 cache
  • 原文地址:https://www.cnblogs.com/zhangfengshi/p/12104147.html
Copyright © 2020-2023  润新知