0

Mongodb 4 added multi document transaction support.

Mongo-scala-driver (http://mongodb.github.io/mongo-scala-driver/2.4/) supports mongodb 4, but I cannot find any example how to use transaction with scala.

Can anybody provide the link or code snippet?

P.S: There is synchronous transaction example in the official mongodb site, but I need example of async, non-blocking transaction in scala.

Teimuraz
  • 8,795
  • 5
  • 35
  • 62

1 Answers1

3

There is an example in the transaction and drivers documentation under the Scala tab.

There are a few extra caveats / gotchas for scala that are covered in the example code.

  • Each observable within the transaction must be passed the ClientSession
  • Each observable must be subscribed to in order for anything to happen (they are cold observables).
  • Transactions can be retried, if they meet the criteria. An example is provided in the code.

There is no Observable abstraction as of version 2.4.0 but there are plans to simplify the API in the future.

Ross
  • 17,861
  • 2
  • 55
  • 73
  • 1
    I think that as a caveat the limitation that a single host mongodb instance does not support sessions should be added. – dr jerry Dec 28 '18 at 20:00
  • @Ross I still cannot find the way how to abort the transaction in case of failure. Did you try it? – Teimuraz Apr 26 '19 at 22:36