我正在尝试在playframework中将事务与reactmonmongodb一起使用。我该怎么做,或者有任何有关Playframework的文档?
在文档中,您可以找到如何使用MongoDB> 4个事务的示例。
import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.bson.BSONDocument
import reactivemongo.api.DefaultDB
def testTx(db: DefaultDB)(implicit ec: ExecutionContext): Future[Unit] =
for {
dbWithSession <- db.startSession()
dbWithTx <- dbWithSession.startTransaction(None)
coll = dbWithTx.collection("foo")
_ <- coll.insert.one(BSONDocument("id" -> 1, "bar" -> "lorem"))
r <- coll.find(BSONDocument("id" -> 1)).one[BSONDocument] // found
_ <- db.collection("foo").find(
BSONDocument("id" -> 1)).one[BSONDocument]
// not found for DB outside transaction
_ <- dbWithTx.commitTransaction() // or abortTransaction()
// session still open, can start another transaction, or other ops
_ <- dbWithSession.endSession()
} yield ()