4

I am working in java (not is spring) on a mongo db collection. I want to perform some update operations in one transaction, so all or none of the operations will be execute.
I didn't find any simple example of how it can be done. I understand that it related to session in mongo db, but how to create this session? If someone have an example of this scenario in java I will appreciate if he could share.

Thanks, Osnat.

1

1 Answer 1

7

There's a complete example in the mongodb 4 documentation, here.

The usage pattern looks like this:

ClientSession session = client.startSession();
        try {
            session.startTransaction(  ... some tranaction options ... ).build());
            // manipulate data
            session.commitTransaction();
        } catch (MongoCommandException e) {
            session.abortTransaction();
        } finally {
            session.close();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.