Quarkus Panache Mongo 笔交易

Quarkus Panache Mongo Transactions

我知道 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 的活动记录模式。

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

MongoDB with Panache 尚不支持事务,参见 https://quarkus.io/guides/mongodb-panache#transactions

事务支持将在 Quarkus 2.0 中提供(应该在 6 月推出),有了它,您将能够在方法上使用 @Transactional 来标记事务的边界,而无需额外的库需要。

请注意,MongoDB 事务仅在 MongoDB 4.0 版后可用,并且需要一个副本集才能工作。