IllegalStateException: state should be : open

Hello,

I have a Java application that uses spring-boot-starter-data-mongodb 2.7.1(also tries 3.0.0) to connect to MongoDB. The configuration is in XML.

 <mongo:db-factory id="mongoDbFactory" connection-string="mongodb+srv://user:pwd@host/database?retryWrites=true"/>
  <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" >
    <constructor-arg ref="mongoDbFactory" />
  </bean>
  <bean class=
  "org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

The connection has been established when the application starts and I do see that the data has been retrieved from the mongo atlas database. Once, the user request is sent, and I try to get the data I see

 java.lang.IllegalStateException: state should be: open

Please find the log trace here:

java.lang.IllegalStateException: state should be: open
	at com.mongodb.assertions.Assertions.isTrue(Assertions.java:79)
	at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:165)
	at com.mongodb.internal.connection.AbstractMultiServerCluster.getDescription(AbstractMultiServerCluster.java:50)
	at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:144)
	at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:101)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:291)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:183)
	at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:135)
	at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92)
	at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2968)
	at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2692)
	at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.doFind(ExecutableFindOperationSupport.java:220)
	at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.oneValue(ExecutableFindOperationSupport.java:132)
	at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.lambda$getExecution$6(AbstractMongoQuery.java:188)
	at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.doExecute(AbstractMongoQuery.java:152)
	at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:127)
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:160)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:139)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:81)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
	at com.sun.proxy.$Proxy39.findByName(Unknown Source)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
	at com.sun.proxy.$Proxy44.findByName(Unknown Source)
	at edu.illinois.techservices.cloudbroker.core.http.Oauth2StateRepoService.findByName(Oauth2StateRepoService.java:22)
	at edu.illinois.techservices.cloudbroker.box.CoreConnectionState$TokenReader.call(CoreConnectionState.java:360)Preformatted text

This used to work before. I tried to figure out where the connection has lost. Any help would be appreciated.

Thanks,
Prasanna

Here are the logs that shows the connection to mongo atlas is made:

2023-04-27 15:39:13,498 : INFO  : [cluster-ClusterId{value='644a971fd5dd176dd4946323', description='null'}-cloud-dashboard-ohio-shard-00-00.sa7ls.mongodb.net:27017] : org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=cloud-dashboard-ohio-shard-00-00.sa7ls.mongodb.net:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=13, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1240940200, setName='atlas-yyxdyg-shard-0', canonicalAddress=cloud-dashboard-ohio-shard-00-00.sa7ls.mongodb.net:27017, hosts=[cloud-dashboard-ohio-shard-00-00.sa7ls.mongodb.net:27017, cloud-dashboard-ohio-shard-00-01.sa7ls.mongodb.net:27017, cloud-dashboard-ohio-shard-00-02.sa7ls.mongodb.net:27017], passives=[], arbiters=[], primary='cloud-dashboard-ohio-shard-00-01.sa7ls.mongodb.net:27017', tagSet=TagSet{[Tag{name='nodeType', value='ELECTABLE'}, Tag{name='provider', value='AWS'}, Tag{name='region', value='US_EAST_2'}, Tag{name='workloadType', value='OPERATIONAL'}]}, electionId=null, setVersion=2, topologyVersion=TopologyVersion{processId=64494b2a9e2222a038e462d8, counter=4}, lastWriteDate=Thu Apr 27 15:39:12 UTC 2023, lastUpdateTimeNanos=1881230175253897}
2023-04-27 15:39:13,504 : INFO  : [cluster-ClusterId{value='644a971fd5dd176dd4946323', description='null'}-cloud-dashboard-ohio-shard-00-01.sa7ls.mongodb.net:27017] : org.mongodb.driver.cluster : Setting max election id to 7fffffff0000000000000010 from replica set primary cloud-dashboard-ohio-shard-00-01.sa7ls.mongodb.net:27017
2023-04-27 15:39:13,504 : INFO  : [cluster-ClusterId{value='644a971fd5dd176dd4946323', description='null'}-cloud-dashboard-ohio-shard-00-01.sa7ls.mongodb.net:27017] : org.mongodb.driver.cluster : Setting max set version to 2 from replica set primary cloud-dashboard-ohio-shard-00-01.sa7ls.mongodb.net:27017
2023-04-27 15:39:13,505 : INFO  : [cluster-ClusterId{value='644a971fd5dd176dd4946323', description='null'}-cloud-dashboard-ohio-shard-00-01.sa7ls.mongodb.net:27017] : org.mongodb.driver.cluster : Discovered replica set primary cloud-dashboard-ohio-shard-00-01.sa7ls.mongodb.net:27017

Hello @Prasanna_Bale, welcome to the MongoDB community forums.

Based on the error message you provided, it appears that you might be encountering a similar issue to the one described in JAVA-2609: mongo throws IllegalStateException: state should be: open. The error could be caused by another process or thread closing the connection with your MongoDB client, which later results in an error when trying to connect again.

To resolve this issue, you may need to review your application’s code to determine when to keep the connection open and when to close the MongoClient connection. It is recommended to create a single MongoClient instance with a required connection pool and reuse it across multiple threads instead of creating a new MongoClient instance each time you need to access the MongoDB Server deployment.

I tried to replicate the error message by querying on MongoDb after the connection was closed and ran into the same error log It would be helpful if you could trace any changes made to the application code, such as the scope of the MongoClient. For instance, there could be a possible addition or removal of try{}… catch{} code snippet.

Please reach out to us for further questions.

Best regards,
Aasawari

1 Like

Hi, @Prasanna_Bale

check whether the application is closing the MongoClient connection or not

as snippet of connection code is not shared. Let me guess what might be the problem

If the client connection is not closed by your application then you might be using try with resources while initiating a new connection. The try with resources will end the connection as soon as the operation of the try block is executed.

Hope it helps