Spring Data MongoDB 4.0 transactions support

2019-03-04 00:11发布

问题:

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

回答1:

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