Skip to content

Commit

Permalink
Transaction support for MongoDB with Panache
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Mar 15, 2021
1 parent 44c4611 commit a46b900
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 73 deletions.
11 changes: 10 additions & 1 deletion docs/src/main/asciidoc/mongodb-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,14 @@ quarkus.log.category."io.quarkus.mongodb.panache.runtime".level=DEBUG

== Transactions

WARNING: MongoDB offers ACID transactions since version 4.0. MongoDB with Panache doesn't provide support for them.
MongoDB offers ACID transactions since version 4.0.

To use them with MongoDB with Panache you need to annotate the method that starts the transaction with the `@Transactional` annotation.

Transactions can be configured via the `@MongoTransactionConfiguration` that allows to specify the read preference, read concern,
and write concern used during the lifespan of the transaction.

WARNING: Transaction support inside MongoDB with Panache is still experimental.

== Custom IDs

Expand Down Expand Up @@ -890,6 +897,8 @@ public Multi<ReactivePerson> streamPersons() {

TIP: `@SseElementType(MediaType.APPLICATION_JSON)` tells RESTEasy to serialize the object in JSON.

WARNING: Transactions are not supported for Reactive Entities and Repositories.

== Mocking

=== Using the active-record pattern
Expand Down
4 changes: 4 additions & 0 deletions extensions/panache/mongodb-panache-common/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mongodb-panache-common</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-narayana-jta-deployment</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
Expand Down
4 changes: 4 additions & 0 deletions extensions/panache/mongodb-panache-common/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<artifactId>quarkus-jackson</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-narayana-jta</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import java.util.Set;
import java.util.stream.Stream;

import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;

import com.mongodb.ReadPreference;
import com.mongodb.client.ClientSession;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
Expand All @@ -21,6 +23,7 @@

public class CommonPanacheQueryImpl<Entity> {
private MongoCollection collection;
private ClientSession clientSession;
private Bson mongoQuery;
private Bson sort;
private Bson projections;
Expand All @@ -32,14 +35,17 @@ public class CommonPanacheQueryImpl<Entity> {

private Collation collation;

public CommonPanacheQueryImpl(MongoCollection<? extends Entity> collection, Bson mongoQuery, Bson sort) {
public CommonPanacheQueryImpl(MongoCollection<? extends Entity> collection, ClientSession session, Bson mongoQuery,
Bson sort) {
this.collection = collection;
this.clientSession = session;
this.mongoQuery = mongoQuery;
this.sort = sort;
}

private CommonPanacheQueryImpl(CommonPanacheQueryImpl<?> previousQuery, Bson projections, Class<?> documentClass) {
this.collection = previousQuery.collection.withDocumentClass(documentClass);
this.clientSession = previousQuery.clientSession;
this.mongoQuery = previousQuery.mongoQuery;
this.sort = previousQuery.sort;
this.projections = projections;
Expand Down Expand Up @@ -150,7 +156,8 @@ public <T extends Entity> CommonPanacheQueryImpl<T> withReadPreference(ReadPrefe
@SuppressWarnings("unchecked")
public long count() {
if (count == null) {
count = collection.countDocuments(mongoQuery);
Bson query = getQuery();
count = clientSession == null ? collection.countDocuments(query) : collection.countDocuments(clientSession, query);
}
return count;
}
Expand All @@ -162,23 +169,21 @@ public <T extends Entity> List<T> list() {
@SuppressWarnings("unchecked")
private <T extends Entity> List<T> list(Integer limit) {
List<T> list = new ArrayList<>();
FindIterable find = mongoQuery == null ? collection.find() : collection.find(mongoQuery);
Bson query = getQuery();
FindIterable find = clientSession == null ? collection.find(query) : collection.find(clientSession, query);
if (this.projections != null) {
find.projection(projections);
}
if (this.collation != null) {
find.collation(collation);
}
manageOffsets(find, limit);
MongoCursor<T> cursor = find.sort(sort).iterator();

try {
try (MongoCursor<T> cursor = find.sort(sort).iterator()) {
while (cursor.hasNext()) {
T entity = cursor.next();
list.add(entity);
}
} finally {
cursor.close();
}
return list;
}
Expand Down Expand Up @@ -232,4 +237,8 @@ private void manageOffsets(FindIterable find, Integer limit) {
find.limit(limit);
}
}

private Bson getQuery() {
return mongoQuery == null ? new BsonDocument() : mongoQuery;
}
}
Loading

0 comments on commit a46b900

Please sign in to comment.