场景,重新发布项目报错

错误信息如下:

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:162)
        at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41)
        at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:127)
        at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:87)
        at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:258)
        at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:182)
        at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1009)
        at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:470)
        at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:453)
        at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:447)
        at org.springframework.data.mongodb.core.MongoTemplate.lambda$insertDocument$16(MongoTemplate.java:1464)
        at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:553)
        at org.springframework.data.mongodb.core.MongoTemplate.insertDocument(MongoTemplate.java:1458)
        at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:1257)
        at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:1172)
        at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:91)
        at sun.reflect.GeneratedMethodAccessor660.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289)
        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.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:529)
        at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:599)

详解

这个错误是因为mongoTemplate关闭掉了,项目关闭的时候会把对应的mongo连接关闭掉,然后再使用这个mongoTemplate就会报错.

排查

项目正常访问,表示新项目启动了,通过jps查看发现老的项目还在跑,错误日志在往前找到对应的业务代码; 发现是在多线程里面执行的,线程池里面如果还有任务,项目就不会杀掉,会等线程池任务执行完成再整个关闭.

解决

因为我们多线程的任务是定时任务从数据库里面拉出来的,如果直接把程序kill -9掉,没执行的任务也会重新再执行不影响,所以我就直接kill掉了