首页 > 文章列表 > SpringBoot集群环境下唯一编号重复的解决方法是什么

SpringBoot集群环境下唯一编号重复的解决方法是什么

228 2024-12-20

SpringBoot集群环境下唯一编号重复的解决方法是什么

服务器集群环境下生成唯一编号重复问题的解答

在开发过程中,使用springboot mybatis-plus实现序列号生成,在单机环境下没有问题,但在集群环境下却出现了重复的情况。

根据所提供的代码,问题在于事务提交的时机。在使用@transactional(rollbackfor = exception.class)时,整个线程的生命周期都处于同一个事务中。在解锁(unlock)时,事务还没有提交,数据还没有写入数据库,此时其他进程有可能读到无效的数据,从而导致重复。

解决方法:

方法一:使用propagation.requires_new

将@transactional注解修改为:

@transactional(rollbackfor = exception.class, propagation = propagation.requires_new)

propagation.requires_new表示当前方法需要在新事务中运行。这样,方法结束时就会立即提交事务,避免脏数据的问题。

方法二:使用transactiontemplate

使用transactiontemplate手动创建和提交事务,更加灵活和可控。

    @Autowired
    TransactionTemplate template;

    public void test() {
        template.execute(status -> {
            // 数据库更新
            return null;
        });
    }

通过手动提交事务,可以确保数据在方法执行结束后才被写入数据库,从而避免重复问题。

来源:1732892322