state should be open

    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.(ClusterBinding.java:75)
    	at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:71)
    	at com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68)
    	at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221)
    	at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:169)
    	at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:75)
    	at com.mongodb.Mongo.execute(Mongo.java:827)
    	at com.mongodb.Mongo$2.execute(Mongo.java:810)
    	at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515)
    	at com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:508)
    	at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:355)
    	at com.test.db.mongoDB.RetryingMongoCollection.updateOne(RetryingMongoCollection.java:909)
 


最近开发spark+mogo+kafka程序,使用mongo做存储的时候,报了上述错误.功能:想在插入数据前,提前查出业务主键相同的记录,进行累加,求极值后,进行upsert,两个流程分开来写,结果报上述错误,反复误解,最后在一篇文件找到了思路.

原因: mongoclient第一次查询到数据后,被close;而随后upsert操作仍然要使用mongoclient,故报上述错误.

解决: 第一次查询后,mongoclient不进行close回收,而是传递给upsert,这样就不存在mongoclient close后,随后又被调用的问题.


以下为参考文章链接:

https://jira.mongodb.org/browse/JAVA-2411

你可能感兴趣的:(MongoDB,spark)