Wednesday, May 16, 2018

Manual rollback with MongoDB

When there is a need to save different documents at the same time in microservices, or more specifically NoSql.
It is not an easy job, because NoSql does not support transaction like RDBMS.

The multiple phases commit in the previous post able to achieve the transaction-like behavior with MongoDB.

The code snippet below will be demonstrated by Play2.x, MongoDB, Morphia.


JsonNode json = request().body().asJson();
MyDocument md = Json.fromJson(json, MyDocument.class);
MyDocument mdCurrent = MyDocumentRepo.findMyDocumentById( md.getId() );

MyDocumentRepo.save( md );

boolean status = callNextService();

if(status == true) {
  // not doing anything
} else {
  // manually rollback the previous md
  MyDocumentRepo.save( mdCurrent );
}



Done!!

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...