简体   繁体   中英

MongoTimeoutException with Java. No changes before getting the issue

I m facing with this issue:

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.Mongo$4@11ad6cd. Client view of cluster state is {type=REPLICA_SET, servers=[{address=bastaproject-shard-00-01-vo5a5.gcp.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, {address=bastaproject-shard-00-00-vo5a5.gcp.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, {address=bastaproject-shard-00-02-vo5a5.gcp.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]
    at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:389)
    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:108)
    at com.mongodb.Mongo.getConnectedClusterDescription(Mongo.java:887)
    at com.mongodb.Mongo.createClientSession(Mongo.java:877)
    at com.mongodb.Mongo$3.getClientSession(Mongo.java:866)
    at com.mongodb.Mongo$3.execute(Mongo.java:823)
    at com.mongodb.MongoCollectionImpl.executeCount(MongoCollectionImpl.java:215)
    at com.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:183)
    at com.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:178)
    at basta.telegram.MongoDAO.subscribed(MongoDAO.java:31)
    at basta.telegram.BastaBot.onUpdateReceived(BastaBot.java:55)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.telegram.telegrambots.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27)
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:309)

I was able to connect with the database and get document's but now i cant. I did'nt change nothing at all in the code before getting this error.

Here is the code that i use to get the collections:

MongoCollection<Document> collection = conexion.getDatabase().getCollection("users");

And here is the connect method:

MongoClientURI uri = new MongoClientURI(
                    "mongodb+srv://cagide_j:"mypassword"@bastaproject-vo5a5.gcp.mongodb.net/test?retryWrites=true&w=majority");
            this.mongoClient = new MongoClient(uri);
            this.database = mongoClient.getDatabase("bastaProject");

Ensure that your Mongo database server is running and if Mongo is running on a different machine, as it appears to be, that you are connected to the internet. I'm not familiar with mongo.net, but many database as a service or in the cloud DB services have features that allow only IP addresses that are whitelisted to connect. I've seen issues with dynamic IP addresses that suddenly cannot connect. You could also try increasing the timeout.

It's hard to say much more without more complete code.

I often find that these issues are something simple, but not necessarily easy to find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM