Ranjith N
Ranjith N

Reputation: 31

Sessions are not supported by the MongoDB cluster to which this client is connected-springboot-mongodb-transactions

I got a requirement to work with spring mongotemplate transactions. unless criteria meets,will have to roll back the transaction manually. I am getting exception like com.mongodb.MongoClientException: Sessions are not supported by the MongoDB cluster to which this client is connected. How to work with Spring mongodb transactions. Please help me out.

@Autowired
MongoTransactionManager mongoTransactionManager;

@Autowired
TestDao testDao;

@Transactional
public void testMethod() {
    int temp = 0;
    try {
        testDao.saveDatainTempTable();
        testDao.saveMongoData();
        if (temp == 0) {
            System.out.println("=========================");
            TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
            mongoTransactionManager.rollback(TransactionInterceptor.currentTransactionStatus());
        }
    } catch (Exception e) {

    }

}

@Configuration
public class MongoTransactionConfig extends AbstractMongoConfiguration {

    @Bean
    MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
        return new MongoTransactionManager(dbFactory);
    }

    @Override
    protected String getDatabaseName() {
        return "test";
    }

    @Override
    public MongoClient mongoClient() {
        return new MongoClient("127.0.0.1", 27017);
    }
}

Upvotes: 2

Views: 5853

Answers (1)

Mukundhan
Mukundhan

Reputation: 3487

  • On Linux, a default /etc/mongod.conf configuration file is included when using a package manager to install MongoDB.

  • On Windows, a default <install directory>/bin/mongod.cfg configuration file is included during the installation

  • On macOS, a default /usr/local/etc/mongod.conf configuration file is included when installing from MongoDB’s official Homebrew tap.

Add the following config

replication:
   oplogSizeMB: 128
   replSetName: "rs0"
   enableMajorityReadConcern: true

sudo service mongod restart;

mongo;

rs.initiate({
      _id: "rs0",
      version: 1,
      members: [
         { _id: 0, host : "localhost:27017" }
      ]
   }
)

check for the config to be enabled

rs.conf()

we can use the connection URL as

mongodb://localhost/default?ssl=false&replicaSet=rs0&readPreference=primary

docs: config-options single-instance-replication

Upvotes: 1

Related Questions