13

I want to explore the new transaction feature of MongoDB and use Spring Data MongoDB. However, I get the exception message "Sessions are not supported by the MongoDB cluster to which this client is connected". Any hint regarding the config of MongoDB 3.7.9 is appreciated.

The stacktrace starts with:

com.mongodb.MongoClientException: Sessions are not supported by the MongoDB cluster to which this client is connected at com.mongodb.MongoClient.startSession(MongoClient.java:555) ~[mongodb-driver-3.8.0-beta2.jar:na] at org.springframework.data.mongodb.core.SimpleMongoDbFactory.getSession(SimpleMongoDbFactory.java:163) ~[spring-data-mongodb-2.1.0.DATAMONGO-1920-SNAPSHOT.jar:2.1.0.DATAMONGO-1920-SNAPSHOT]

kakabali
  • 3,824
  • 2
  • 29
  • 58
Juergen Zimmermann
  • 2,084
  • 7
  • 29
  • 43
  • You need a MongoDB **"Server"** version 3.6 [in order to use sessions](https://docs.mongodb.com/manual/reference/method/Mongo.startSession/). An updated driver alone does not do the job without the server on the back end. That's what the error is telling you. – Neil Lunn May 09 '18 at 14:20
  • I'm having server version 3.7.9, i.e. the latest. – Juergen Zimmermann May 09 '18 at 15:43
  • There is no such thing as "server" 3.7.9. That's the **Java driver version**. The "server" means the thing you see when you connect via the mongo shell and type `db.version()`. Even if you were using a development branch server then the current "cut" is 3.7.5. So you're talking about the "driver" and I'm talking about the "server". – Neil Lunn May 09 '18 at 21:34
  • I downloaded `http://downloads.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.7.9.zip?_ga=2.134458040.816321646.1525581231-1505195349.1522993255` and `db.version()` inside the mongo shell returns `3.7.9` – Juergen Zimmermann May 10 '18 at 04:27
  • Well that is a development release but the driver certainly says the actual connection is not in fact a supported server, so you probably should check at least that you are actually connecting to the server instance you think you are connecting to. Also you probably should not be using a development release until you actually have working code against a "stable" release anyway. As already stated, you "should" be using a 3.6 series server in order to use sessions. Current driver and server releases in the "development" branch are in "flux" with various things in API likely to change. – Neil Lunn May 10 '18 at 04:42

6 Answers6

15

I was having the same issue when I was trying to connect it to a single standalone mongo instance, however as written in the official documentation, that Mongo supports transaction feature for a replica set. So, I then tried to create a replica set with all instances on MongoDB 4.0.0, I was able to successfully execute the code. So, Start a replica set (3 members), then try to execute the code, the issue will be resolved.

NB : you can configure a replica set on the same machine for tests https://docs.mongodb.com/manual/tutorial/deploy-replica-set-for-testing/

Piyush Verma
  • 399
  • 2
  • 7
8

We were able to config in local as below

  • 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

Mukundhan
  • 3,284
  • 23
  • 36
5

Replica set is the resolution for the issue for sure

But doing replica of 3 nodes is not mandatory.

Solution 1 (for standalone setup)

For standalone mongo installation you can skip configuring 2nd or 3rd node as described on the official mongo documentations here

And you'll need to set a replSetName in the configuration

replication:
   oplogSizeMB: <int>
   replSetName: <string>
   enableMajorityReadConcern: <boolean>

and then run details of which are here

rs.initiate()

after this the connection string would be like below:-

mongodb://localhost:27017/<database_name>?replicaSet=<replSet_Name>

keys above that you need to replace:-

database_name = name of the database

replSet_Name = name of the replica set you setup in the above configuration

Solution 2 (only for docker based requirement)

Example Docker image with single node replica set acting as primary node for development environment is as below:-

I had hosted the docker image on the docker hub

docker pull krnbr/mongo:latest

Contents of the same Dockerfile are below:-

FROM mongo
RUN echo "rs.initiate({'_id':'rs0','members':[{'_id':0,'host':'127.0.0.1:27017'}]});" > /docker-entrypoint-initdb.d/replica-init.js
RUN cat /docker-entrypoint-initdb.d/replica-init.js
CMD [ "--bind_ip_all", "--replSet", "rs0" ]

Docker run command (replace with the image name that you build yoursef or use the on shared above i.e krnbr/mongo):-

without volume


docker run -d --name mongo -p 27017:27017 <Image Name> mongod --replSet rs0 --port 27017

with volume


docker run -d --name mongo -p 27017:27017 -v ~/.mongodb:/data/db <Image Name> mongod --replSet rs0 --port 27017

for supporting binding of any ip

docker run -d --name mongo -p 27017:27017 -v ~/.mongodb:/data/db <Image Name> mongod --bind_ip_all --replSet rs0 --port 27017
kakabali
  • 3,824
  • 2
  • 29
  • 58
  • @kakabaly I just tried to convert my standalone setup to a single node replica set following you're answer nut now I have `ECONNREFUSED ::1:27012` when connecting to it. opened a question here https://stackoverflow.com/questions/70322250/mongooseserverselectionerror-connect-econnrefused-127017-when-connecting-to , if you could check it and see where I got it wrong I'd really appreciate it. PD I edited the mongod.conf file with Atom and saved it. Many thanks – Vincenzo Dec 12 '21 at 10:02
0

With the reference to the answer give by @kakabali, I have few a bit different scenario and configure it.

I am configure mongo with spring boot and try to use transactions management and getting the error:

com.mongodb.MongoClientException: Sessions are not supported by the MongoDB cluster to which this client is connected at

I follow few of the steps given by above answer and added few:

Change the mongo.cfg and added this

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

Restart the service as I am using Windows10.

Open mongo console and run rs.initilize()

Atul
  • 3,043
  • 27
  • 39
-1

I disabled TLS (inside Spring Data MongoDB), and now the transaction feature with the developement release 3.7.9 works fine.

Juergen Zimmermann
  • 2,084
  • 7
  • 29
  • 43
-1

Make sure you're using the updated API - for example:

MongoClient mongoClient = MongoClients.create();
MongoDatabase dataBase = mongoClient.getDatabase("mainDatabase");
MongoCollection<Document> collection = dataBase.getCollection("entities");

Also make sure you have mongo.exe open.

Guy
  • 145
  • 3
  • 12