Atlas Prematurely reached end of stream error

3,919 views
Skip to first unread message

Subhash Chandra

unread,
Jul 15, 2018, 5:59:57 PM7/15/18
to mongodb-user

I get a Prematurely reached end of stream error when trying to connect to MongoDB Atlas Free Tier. Using Java 1.8 and Mongo Driver 3.8.0. Does this have any thing to do with the Cluster setup in Atlas? I got the same program working with my MongoDB localhost.

Any Idea?


Java Test class


import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongoTest {

public static void main(String args[]) {

    MongoClientURI uri = new MongoClientURI(
            "mongodb://username:password@cluster0-****.mongodb.net");

    MongoClient mongoClient = new MongoClient(uri);
    MongoDatabase database = mongoClient.getDatabase("account");

    System.out.println("Hello");

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

    System.out.println(collection.count());

    mongoClient.close();

  }
}

Error:

    Jul 15, 2018 12:32:04 PM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Cluster created with settings {hosts=[cluster0-****.mongodb.net:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
    Hello
    Jul 15, 2018 12:32:04 PM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
    Jul 15, 2018 12:32:04 PM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Exception in monitor thread while connecting to server cluster0-****.mongodb.net:27017
    com.mongodb.MongoSocketReadException: Prematurely reached end of stream
	at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:92)
	at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:554)
	at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:425)
	at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:289)
	at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
	at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83)
	at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33)
	at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:106)
	at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:63)
	at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
	at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
	at java.lang.Thread.run(Thread.java:748)

    Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=cluster0-****.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]
	at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:179)
	at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41)
	at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:136)
	at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:94)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:249)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:172)
	at com.mongodb.client.internal.MongoCollectionImpl.executeCount(MongoCollectionImpl.java:248)
	at com.mongodb.client.internal.MongoCollectionImpl.count(MongoCollectionImpl.java:183)
	at com.mongodb.client.internal.MongoCollectionImpl.count(MongoCollectionImpl.java:171)
	at MongoTest.main(MongoTest.java:21) 

Wan Bachtiar

unread,
Aug 7, 2018, 3:50:31 AM8/7/18
to mongodb-user

Does this have any thing to do with the Cluster setup in Atlas? I got the same program working with my MongoDB localhost.

Hi Subhash,

Based on the log you provided, looks like the client is having a difficulty in connecting to MongoDB Atlas.

Looking at your connection string URI, you’re using the SRV address, but the prefix for the connection format is only showing mongodb://, try changing this to mongodb+srv://. If you’re using the MongoDB 3.4 or earlier format, but obfuscated the URI just to post the question, check whether you’ve also specified ssl=true and the replicaSet parameter.

Also, ensure that you have added to the whitelist the IP of the application in MongoDB Atlas.

Regards,
Wan.

Punit Choudhary

unread,
Mar 1, 2019, 11:28:31 AM3/1/19
to mongodb-user
i am getting the same problem but when i used +srv it showed the error that string must begin with mongo:// 
i didn't get the resolution of my problem till now

Wan Bachtiar

unread,
Mar 6, 2019, 1:48:40 AM3/6/19
to mongodb-user

i am getting the same problem but when i used +srv it showed the error that string must begin with mongo://

Hi Punit,

Although you may be getting a similar error message it may be caused by a different reason. If you’re still having this issue, please open a new discussion with the following information:

  • MongoDB Java driver
  • MongoDB server version
  • The complete error stack trace that you’re getting

Regards,
Wan.

Uttiya Pal

unread,
Jun 13, 2019, 2:03:34 AM6/13/19
to mongodb-user
I have faced this similar issue. Its weird.


On Monday, 16 July 2018 03:29:57 UTC+5:30, Subhash Chandra wrote:

Wan Bachtiar

unread,
Jun 14, 2019, 1:03:15 AM6/14/19
to mongodb-user

I have faced this similar issue. Its weird.

Hi Uttiya,

Check your network connectivity, and make sure you’re able to connect from the application instance to the database server.

Also similarly as previous post, although you may be getting a similar error message it may be caused by a different reason. If you’re having this issue, please open a new discussion thread with the following information:

  • MongoDB Java driver version
  • MongoDB server version
  • The complete error stack trace that you’re getting

    Regards,
    Wan.

    Reply all
    Reply to author
    Forward
    0 new messages