[JAVA-2794] Spring MongoTemplate through exception while accessing through spring @async(multithreading) method Created: 27/Feb/18  Updated: 27/Oct/23  Resolved: 27/Feb/18

Status: Closed
Project: Java Driver
Component/s: Async, Connection Management
Affects Version/s: 3.5.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Swapnadarsan moharana Assignee: Ross Lawley
Resolution: Works as Designed Votes: 0
Labels: driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 64 bit.


Issue Links:
Problem/Incident
is caused by JAVA-2411 MongoDB: java.lang.IllegalStateExcept... Closed

 Description   

Hi,

Got this error while trying to aggregate through spring data mongoDB in a Spring async method.

java.lang.IllegalStateException: state should be: open
	at com.mongodb.assertions.Assertions.isTrue(Assertions.java:70)
	at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:82)
	at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
	at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
	at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
	at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:90)
	at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:85)
	at com.mongodb.operation.CommandReadOperation.execute(CommandReadOperation.java:55)
	at com.mongodb.Mongo.execute(Mongo.java:836)
	at com.mongodb.Mongo$2.execute(Mongo.java:823)
	at com.mongodb.DB.executeCommand(DB.java:729)
	at com.mongodb.DB.command(DB.java:491)
	at com.mongodb.DB.command(DB.java:507)
	at com.mongodb.DB.command(DB.java:462)
	at org.springframework.data.mongodb.core.MongoTemplate$3.doInDB(MongoTemplate.java:410)
	at org.springframework.data.mongodb.core.MongoTemplate$3.doInDB(MongoTemplate.java:408)
	at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:466)
	at org.springframework.data.mongodb.core.MongoTemplate.executeCommand(MongoTemplate.java:408)
	at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1576)
	at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1511)
	at com.metlife.impactview.service.DBComputationImpl.getPOIStreamByReferenceId(DBComputationImpl.java:616)
	at com.metlife.impactview.service.MultiLevelAsyncServiceImpl.createJobLevelGraphForAllStreams(MultiLevelAsyncServiceImpl.java:188)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
	at com.sun.proxy.$Proxy119.createJobLevelGraphForAllStreams(Unknown Source)
	at com.metlife.impactview.service.TestService.tester(TestService.java:75)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
	at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)



 Comments   
Comment by Ross Lawley [ 27/Feb/18 ]

Hi darsan55,

I think that this is something you should either post on Stackoverflow or reach out to the Spring Team as how to achieve what you are after.

The cause of the error is as described above and I'd hazzard a guess one thread has closed the client.

Ross

Comment by Swapnadarsan moharana [ 27/Feb/18 ]

Hi Roos,

#1below is the spring function from where i m doing aggregation.

public List<JobStream> getPOIStreamByReferenceId(String refStreamId){
		List<JobStream> poiStreamList = new ArrayList<>();
		try {
			if(null != refStreamId && refStreamId.trim().length() != 0 ) {
				
				String[] refStreamArr = refStreamId.split(getProperty.getFaceSign());
				
				if(refStreamArr.length == 3) {
					String streamName = refStreamArr[0];
					String workstationName = refStreamArr[1];
					String startTime = utilityService.convertESTToUTC(refStreamArr[2]);
					//System.out.println("Array : "+refStreamArr[0]);
					//here startTime should be in UTC time.
					{color:red}MatchOperation nameMatch = Aggregation.match(new Criteria(getProperty.getKeyName()).is(streamName));
					MatchOperation workStationMatch = Aggregation.match(new Criteria(getProperty.getKeyWorkStationName()).is(workstationName));
					MatchOperation startTimeMatch = Aggregation.match(new Criteria(getProperty.getKeyStartTime()).regex(startTime));
					
					Aggregation aggregation = Aggregation.newAggregation(nameMatch,workStationMatch,startTimeMatch);
					AggregationResults<JobStream> result = mongoTemplate.aggregate(aggregation, getProperty.getPointInTimeCollection(), JobStream.class);{color}
					
					List<JobStream> mappedResult = result.getMappedResults();
					
					if( null != mappedResult && !mappedResult.isEmpty()) {
						poiStreamList = mappedResult;
					}
				}
			}
		}

The above method i m calling from a async method from inside a loop. so every call will be a separate thread.

Comment by Ross Lawley [ 27/Feb/18 ]

Hi darsan55,

That error occurs when the Cluster is used after it has been closed. This is often indicitive of closing the MongoClient in one thread, while still using it in another.

At the moment I'm inclined to close this as "Works As Designed" but will wait for more feedback to confirm the cause of the error. It could be possible its a Spring issue, however, we haven't heard any similar reports.

Please provide more information and if possible a test case reproducing the error and I'll follow up.

Ross

Generated at Thu Feb 08 08:58:05 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.