如何使用 mysql 代替 redis 长时间持久化计数

保证单线程:工具类方法使用synchronized,保住REQUIRES_NEW事务,保证全局线程安全,可以使用分布式锁,加同步锁降低复杂度

保证与调用方事务隔离使用REQUIRES_NEW,REQUIRES_NEW执行完会立即提交,腾出资源供供其他模块调用,避免长时间等待调用方提交事务才结束


​​​​​​​public class MysqlSysCountUtils {
    public static final String BomVersionCountKey = "xxxxx:";
    public static final String BomVersionModelName = "xxxxx";

    //synchronized 必须加在@Transction外面
    public static synchronized Integer getSysNo(String modelName, String counterKey) {
        return SpringUtils.getBean(ISysCounterService.class).getSysNo(modelName, counterKey);
    }
}


// 通用计数器(永久存储技术)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public  Integer getSysNo(String modelName, String counterKey) {
    SysCounter sysCounter = getOrCreateSysCounterForCommon(modelName,counterKey);
    Integer increment = sysCounter.getCounterValue();
    increment++;

    SysCounter  update = new SysCounter();
    update.setId(sysCounter.getId());
    update.setCounterValue(increment);
    baseMapper.updateById(update);
    return increment;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值