Sajitha Liyanage
Sajitha Liyanage

Reputation: 473

MongoDB: java.lang.IllegalStateException: state should be: ClientSession from same MongoClient

I have an application that stores data in the used MongoDB Atlas (Cluster Tier: M0 Sandbox Shared RAM, 512 MB Storage) collection as a transaction. The sample code is as follows.

MongoClient mongoClient = MongoClients.create("Connection-URL");
MongoDatabase mongoDatabase = mongoClient.getDatabase("DATABASE");

ClientSession clientSession = mongoClient.startSession();
clientSession.startTransaction();
status = mongoDatabase.getCollection(collection).insertOne(clientSession, insertDocument);

if (status == IS_SUCCESS) {
   clientSession.commitTransaction();
} else {
   clientSession.abortTransaction();
}

clientSession.close();

This works fine with multiple requests. However, after some time (can not say the exact time), I'm getting the below exception if I invoke the above functionality.

java.lang.IllegalStateException: state should be: ClientSession from same MongoClient
    at com.mongodb.assertions.Assertions.isTrue(Assertions.java:72)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:279)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:206)
    at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1053)
    at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:503)
    at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:499)
    at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:492)
    at mongo.impl.InsertDocuments.insertItem(InsertDocuments.java:24)

and continues failing all the time...

I'm using the following client connector to connect from my JAVA application

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.12.8</version>
</dependency>

Does anyone know why this is happening?

Upvotes: 2

Views: 1852

Answers (1)

Sajitha Liyanage
Sajitha Liyanage

Reputation: 473

I no longer encountered the previously mentioned error after setting the MongoDB client as follows (use this in a Singleton class).

ConnectionString connectionString = new ConnectionString("mongoUrl");
CodecRegistry pojoCodecRegistry = CodecRegistries.fromProviders(PojoCodecProvider.builder().automatic(true).build());
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
MongoClientSettings clientSettings = MongoClientSettings.builder()
                    .retryWrites(true)
                    .applyConnectionString(connectionString)
                    .codecRegistry(codecRegistry)
                    .applyToConnectionPoolSettings((ConnectionPoolSettings.Builder builder) -> {
                        builder.maxSize(mongoMaxPoolSize) //connections count
                                .minSize(5)
                                .maxConnectionLifeTime(30, TimeUnit.MINUTES)
                                .maxConnectionIdleTime( 30000, TimeUnit.MILLISECONDS);
                    })
                    .applyToSocketSettings(builder -> {
                        builder.connectTimeout(2000, TimeUnit.MILLISECONDS);
                    })
                    .build();
mongoClient = MongoClients.create(clientSettings);

Upvotes: 1

Related Questions