MongoSocketReadException when used Hibernate OGM with cloud MongoDB Atlas


I was asked this question on

I am trying to use MongoDB Atlas M0 (Free Tier) for my JAVA EE application, now I am using:

  • Local MongoDB database (v4.0.4)

  • Hibernate Core "hibernate-core 5.3.6.Final"

  • Hibernate OGM "hibernate-ogm-mongodb 5.3.1.Final"

  • Java application server WildFly 15.0.0.Final

When I tried to connect Hibernate with Mongo Atlas to test cloud database, I am was not able to have a working connection, because mongodb driver throws an exception com.mongodb.MongoSocketReadException: Prematurely reached end of stream

My persistence.xml that causes the error:

As @Davide D'Alto suggested I create Factory to interact for the persistence unit




name surname
February 5, 2019, 9:38 PM

Hi Davide!
I was investigating a little bit more a I believe I found the problem. Finally I was able to make connection from my own MYCustomMongoDBDatastoreProvider (that you suggested me) only with make a connection with MongoClientOptions option sslEnabled(true), to do this I should create MongoClient and pass to it all options and configs like this:

So without sslEnabled(true) I had the same error as before, but with it works fine.

And I also found this issue where people had the same problems and as I was able to understand, they spoke about SSL problem too.

In fact here - "Connect via Driver" there is Prerequisites:

I can't do objective conclusions, but may be this error is throws by lack of implementation SSL.

name surname
February 5, 2019, 7:07 PM

yeah, got it! And then I've created this class MYCustomMongoDBDatastoreProvider and then overrode createMongoClient and tried create MongoClient just simple like this to connect to one node:

or this to connect to the cluster of 3 nodes:

But nothing changed, like before I was received the same error:

Davide D'Alto
February 5, 2019, 5:23 PM

Sure, make sure once you have created the class to change this property:

to something like this:

name surname
February 5, 2019, 5:09 PM

Hi Davide!
I am now on it, trying to use 'MYCustomMongoDBDatastoreProvider' like you suggested to me! I am not very familiar with this kind of using of connection, that's why it takes a bit of time for me. Thank you for your help!

Davide D'Alto
February 5, 2019, 5:02 PM

Sorry, I cannot understand form the description... Are you saying that using that the creations of a custom `MongoDBDatastoreProvider` that uses `MongoClients.create(...)` worked for you? Thanks




name surname