I was finally trying out mongodb transactions with a local single node replicaset (version 4.2.3 MacOS), however I was unable to rollback any modifications I made within that transaction. I am using the reactive java driver version 1.13.0.
As I was unable to reproduce expected behaviour, I have created a simple Spock test which would insert a document, abort the transaction and then try to find the just inserted document in a new session (just to make sure, however I am able to see identical results if I add breakpoints and use the CLI to verify)
class MongoSpecification extends Specification {
MongoClient mongoClient;
MongoCollection collection;
def setup() {
mongoClient = MongoClients.create();
MongoDatabase db = mongoClient.getDatabase("test");
collection = db.getCollection("test");
AsyncConditions conditions = new AsyncConditions(1)
// make sure to drop the db to start with a clean state
Single.fromPublisher(db.drop()).subscribe({ s ->
conditions.evaluate({
s != null
})
}, { t -> t.printStackTrace()})
conditions.await()
}
def 'test'() {
when:
AsyncConditions conditions = new AsyncConditions(1)
Single.fromPublisher(mongoClient.startSession()).flatMap({session ->
session.startTransaction();
return Single.fromPublisher(collection.insertOne(new Document("_id", 1).append("msg", "test")))
.map({ s ->
System.out.println("aborting transaction");
session.abortTransaction();
return s;
})
}).subscribe({ success ->
System.out.println("insert result: " + success);
conditions.evaluate({
success != null
})
}, { t ->
t.printStackTrace()
})
then:
conditions.await();
when:
conditions = new AsyncConditions(1)
Single.fromPublisher(mongoClient.startSession()).flatMap({session ->
return Single.fromPublisher(collection.find(new Document("_id", 1)).first())
}).subscribe({ document ->
System.out.println("found document: " + document);
conditions.evaluate({
document != null
})
}, { t ->
t.printStackTrace();
})
then:
conditions.await()
}
}
The output is:
aborting transaction
insert result: SUCCESS
found document: [_id:1, msg:test]
If I check session.hasActiveTransaction() before aborting the transaction, it returns true.
The mongo driver lists transaction support since version 1.9.0, so there must be something wrong how I try to handle transaction within the reactive context. Unfortunately I was unable to find any documentation regarding the use of the reactive driver together with transactions.
Does anyone have an idea what is going on here?

