数据库读写分离带来的问题


线上楼层错误问题


1.0版本的代码,先查最大楼层号,测试环境怎么测都没问题

//查询最大楼层号
        Comments latestComment = commentsDAO.selectFirstByPostId(post.getId());
        int maxFloorNum = 0;
        if(latestComment != null) {
            maxFloorNum = latestComment.getFloorNum();
        }
        logger.debug("addPostComment: the post_id {} maxFloorNum is {}", post.getId(), maxFloorNum);
        //(2) 将回复插入到comments表
        Comments comment = PostCommentConvert.toPostComment(commentReqBO);
        comment.setForumCode(post.getForumCode());
        comment.setFloorNum(maxFloorNum+1);
        int num = commentsDAO.insertSelective(comment);


后来线上出了问题,以为是并发造成的,于是在发评论的时候加了锁

Comments comment = null;
        int num = 0;
        int sleepTimeMillis = 500, maxWaitSeconds = 5;//超时时间
        //当前时间
        long  startTime = System.currentTimeMillis();
        while (true) {
            try {
                //redis获取锁
                simpleLock.tryLock(KeyBuilder.getKeySocialPostCommentLock());
            } catch (YHLockFailedException e) { //尝试获取锁
                try {
                    Thread.sleep(sleepTimeMillis);
                } catch (InterruptedException e1) {}
                long costTime = System.currentTimeMillis() - startTime;
                if(costTime > TimeUnit.SECONDS.toMillis(maxWaitSeconds)) {
                    logger.warn("addPostComment: add timeout, post_id {}, millisecond time is {}", post.getId(), costTime);
                    throw new ServiceException(500, "添加评论超时");
                }
                continue;
            }
            break;
        }


结果发现,还是有问题。这就纳闷了,后来询问以前做这个项目的人,他说这个项目是读写分离的,心想那估计是这个问题了,于是用redis来操作

//查询最大楼层号 + 1
int maxFloorNum = (int) countCache.incrementPostCommentFloorNum(post.getId(), 1);

public long incrementPostCommentFloorNum(Integer postId, Integer num) {

    logger.info("enter incrementPostCommentFloorNum. param postId is {}, num is {}", postId, num);
    // (1)校验forumCode
    if (null == postId || 1 > postId) {
        return 0;
    }
    String cacheKey = KeyBuilder.getKeySocialPostCommentFloorMaxNum(postId);
    long result = 0;
    // (2)判断缓存中是否有记录,没有记录则同步数据库数据,有则直接increment
    if (redisValueCache.setIfAbsent(cacheKey, 0, COMMENTS_NUM_IN_SEC, TimeUnit.MINUTES)) {
        Comments latestComment = commentsDAO.selectFirstByPostId(postId);
        int maxFloorNum = 1;
        if (latestComment != null) {
            maxFloorNum = latestComment.getFloorNum() + 1;
        }
        result = redisValueCache.increment(cacheKey, new Long(maxFloorNum), COMMENTS_NUM_IN_SEC, TimeUnit.MINUTES);
    } else {
        result = redisValueCache.increment(cacheKey, num, COMMENTS_NUM_IN_SEC, TimeUnit.MINUTES);
    }
    return result;
}

至此问题解决,用到读写分离一定要注意!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值