码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-12-26 21:30:04      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:ntc   update   ogg   ==   注意   err   type   close   date   

 @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]

 

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

标签:ntc   update   ogg   ==   注意   err   type   close   date   

原文地址:https://www.cnblogs.com/zhangfengshi/p/12104147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!