0

我知道对 MongoDB 的事务支持仍处于试验阶段……但我正试图在我的一个项目中使用它,该项目仍处于早期阶段。

由于我没有使用 Hibernate ...我还添加了 JTA 依赖项,如下所示:

    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-mongodb-panache</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-narayana-jta</artifactId>
    </dependency>

我正在使用 @Transactional 注释,就像:

@POST
@Transactional
public Response addServicePlace(@RequestBody(description = "Adds a new record", required = true, content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = ServicePlaces.class))) @Valid ServicePlaces services, @Context UriInfo uriInfo) {

    Service s = new Service();
    s.typeId = services.type;
    s.title = services.title;
    s.persist();

    services.descriptions.stream().forEach(c -> {
       ServiceDescription serviceDescription = new ServiceDescription();
       serviceDescription.lang = c.lang;
       serviceDescription.description = c.description;
       serviceDescription.serviceId = s.id;
       serviceDescription.persist();
    });

    throw new RuntimeException("BANG!"); ....

但是,事务不会回滚。

我也使用了声明性事务实现 - 相同的行为。

作为参考,我正在使用 Panache 的 Active Record 模式。

有人遇到过类似的情况吗?

4

1 回答 1

1

带有 Panache 的 MongoDB 尚不支持事务,请参阅https://quarkus.io/guides/mongodb-panache#transactions

Quarkus 2.0 将提供交易支持(应该在 6 月推出),有了它,您将能够使用@Transactional一种方法来标记您的交易边界,不需要额外的库。

请注意,MongoDB 事务仅从 MongoDB 版本 4.0 开始可用,并且需要副本集才能工作。

于 2021-04-20T10:28:07.247 回答