0

MongoDB 4.0 are going to introduce transactions support with ACID guarantees.

Does Spring Data MongoDB already supports the transactions in MongoDB and if no, when this awesome feature will be available. I really need it, taking into account the following issue - MongoDB schema design in order to support application horizontal scaling

kakabali
  • 3,824
  • 2
  • 29
  • 58
alexanoid
  • 24,051
  • 54
  • 210
  • 410

2 Answers2

2

Does Spring Data MongoDB already supports the transactions in MongoDB

Spring Data Lovelace M3 (2.1.0.M3) supports synchronous transaction for MongoDB v4.0, released on May 17th 2018. See also Spring Data Lovelace M3 release notes.

Example from Spring Data docs: MongoDB transactions

ClientSession session = client.startSession(options);                   

template.withSession(session)
    .execute(action -> {
        session.startTransaction();                                     
        try {

            Step step = // ...;
            action.insert(step);
            process(step);
            action.update(Step.class).apply(Update.set("state", // ...
            session.commitTransaction();                                
        } catch (RuntimeException e) {
            session.abortTransaction();                                 
        }
    }, ClientSession::close)                                            
    .subscribe();

See also related: DATAMONGO-1920 and DATAMONGO-1970

Wan B.
  • 18,367
  • 4
  • 54
  • 71
  • Thanks for your answer! Do you have an idea when it will be released and included into some Spring Boot RELEASE ? – alexanoid Jun 14 '18 at 07:34
  • @WanBachtiar Does Spring Data supports transaction support for mongodb that is older than version 4.0. – Faraz Aug 22 '18 at 07:26
  • @Desert no on the database level. You _may_ be able to use software level transactions with Spring [Transaction Management](https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#transaction) I would just recommend to upgrade to 4.0 to simplify it if you're able to upgrade. – Wan B. Sep 12 '18 at 06:28
1

You can refer the answer that I recently answered on another thread, I hope that will be helpful to you.

But that is for reactive style Spring Boot and MongoDB setup

The link is here

kakabali
  • 3,824
  • 2
  • 29
  • 58