Spring Boot 使用dev.miku在R2BC for MYSQL中处理事务

fgw7neuy  于 2023-04-11  发布在  Spring
关注(0)|答案(1)|浏览(422)

我试图实现简单的事务机制,只是为了检查它是如何在React世界中使用Mysql的dev.miku,但得到下面的错误

Receiver class dev.miku.r2dbc.mysql.MySqlConnection does not define or inherit an implementation of the resolved method 'abstract org.reactivestreams.Publisher beginTransaction(io.r2dbc.spi.TransactionDefinition)'

我写的代码如下

@Autowired
private TransactionalOperator operator;

public Mono<ServerResponse> insertUserData(ServerRequest serverRequest) {
    return serverRequest.bodyToMono(UserAndFamilyRequest.class)
            .flatMap(saveFamilyMembers())
            .as(operator::transactional)  //using transactional operator
            .flatMap(familyMemberEntity -> ServerResponse.ok()
                    .contentType(MediaType.APPLICATION_JSON)
                    .bodyValue(familyMemberEntity));
}

private Function<UserAndFamilyRequest, Mono<List<FamilyMemberEntity>>> saveFamilyMembers() {
    return userAndFamilyRequest -> {
        User user = userAndFamilyRequest.getUser();
        UserEntity userEntity = new UserEntity();
        userEntity.setName(user.getName());
        userEntity.setAge(user.getAge());
        userEntity.setSeats(user.getSeats());
        userRepo.save(userEntity);
        List<FamilyMember> list = userAndFamilyRequest.getFamilyMemberList();
        var entityList = list.stream().map(familyMember -> {
            FamilyMemberEntity familyMemberEntity = new FamilyMemberEntity();
            familyMemberEntity.setName(familyMember.getName());
            familyMemberEntity.setAge(familyMember.getAge());
            return familyMemberEntity;
        }).collect(Collectors.toList());
        return familyRepo.saveAll(entityList).collect(Collectors.toList());
    };
}

我尝试用@Transactional注解insertUserData方法,但得到同样的错误。

<dependency>
            <groupId>dev.miku</groupId>
            <artifactId>r2dbc-mysql</artifactId>
            <version>0.8.2.RELEASE</version>
</dependency>

我是否需要为我的目的导入一些其他依赖项,或者我做错了什么吗

ogq8wdun

ogq8wdun1#

问题的根本原因是dev.miku:r2dbc-mysql:0.8.2没有实现beginTransaction(TransactionDefinition)方法。这个版本的r2 dbc-mysql非常过时(到目前为止已经3年了),并且不再维护。
我建议根据您的Spring Data版本尝试io.asyncer:r2dbc-mysql:0.9.1io.asyncer:r2dbc-mysql:1.0.0io.asyncer:r2dbc-mysqldev.miku:r2dbc-mysql的官方继承者。

相关问题