简体   繁体   English

Quarkus Panache Mongo 交易

[英]Quarkus Panache Mongo Transactions

I know that Transaction support for MongoDB is still in experimental territory... but I'm trying to use it in one of my projects that is still also in some early stages.我知道对 MongoDB 的事务支持仍处于试验阶段……但我正试图在我的一个项目中使用它,该项目仍处于早期阶段。

As I'm not using Hibernate... I've added also the JTA dependency just like so:由于我没有使用 Hibernate ...我还添加了 JTA 依赖项,如下所示:

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

I'm using @Transactional annotation just like:我正在使用 @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!"); ....

However the transaction is not rollbacked.但是,事务不会回滚。

I have used also declarative transactional implementation - same behaviour.我也使用了声明性事务实现 - 相同的行为。

For reference I'm using Panache's Active Record Pattern.作为参考,我正在使用 Panache 的 Active Record 模式。

Did someone faced a similar scenario?有人遇到过类似的情况吗?

MongoDB with Panache didn't support transactions yet, see https://quarkus.io/guides/mongodb-panache#transactions .带有 Panache 的 MongoDB 尚不支持事务,请参阅https://quarkus.io/guides/mongodb-panache#transactions

Transaction support will be provided in Quarkus 2.0 (should be out in June), with it, you will be able to use @Transactional on a method to mark the boundaries of your transaction, no additional libraries will be needed. Quarkus 2.0 将提供事务支持(应该在 6 月推出),有了它,您将能够在方法上使用@Transactional来标记您的事务边界,不需要额外的库。

Be careful that MongoDB transactions are available since MongoDB version 4.0 only and needs a replicaset to work.请注意,MongoDB 事务自 MongoDB 版本 4.0 起可用,并且需要副本集才能工作。

Hi loicmathieu,嗨,loicmathieu,

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

//repo 
@ApplicationScoped
public class ProjectRepository implements 
PanacheMongoRepositoryBase<Project, String> {
....
}

//transaction method
@Inject 
ProjectRepository projectRepository
....


@Transactional
public Project import(Order order, User user) {
    Project project = factor.from(order);
    projectRepository.persist(project);

    if (project.id != null){
      //try to throw exception manually
      throw new RuntimeException("testing");
    }
    return project;
}

I have tried @transactional annotation with mongodb 4.4.10 and quarkus 2.5.1.我已经尝试使用 mongodb 4.4.10 和 quarkus 2.5.1 进行 @transactional 注释。 Transaction still has no rollback effect.事务仍然没有回滚效果。 any ideas?有任何想法吗? thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM