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.
As I'm not using Hibernate... I've added also the JTA dependency just like so:
<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:
@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.
Did someone faced a similar scenario?
MongoDB with Panache didn't support transactions yet, see 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.
Be careful that MongoDB transactions are available since MongoDB version 4.0 only and needs a replicaset to work.