保证单线程:工具类方法使用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;
}

被折叠的 条评论
为什么被折叠?



