This issue can't be edited
Because it belongs to an archived project. Jira admins can restore projects from the archive.
MongoSocketReadException when used Hibernate OGM with cloud MongoDB Atlas
Activity
name surname February 5, 2019 at 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:
ServerAddress seed1 = new ServerAddress("cluster0-clustername-shard-00-00-raa4n.mongodb.net", 27017);
ServerAddress seed2 = new ServerAddress("cluster0-clustername-shard-00-01-raa4n.mongodb.net", 27017);
ServerAddress seed3 = new ServerAddress("cluster0-clustername-shard-00-02-raa4n.mongodb.net", 27017);
// Construct the Seed list.
List<ServerAddress> seedList = new ArrayList<ServerAddress>();
seedList.add(seed1);
seedList.add(seed2);
seedList.add(seed3);
// Construct authentication information.
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(MongoCredential.createScramSha1Credential("atlas-user-name", "admin", "atlas-user-password".toCharArray()));
// For the construction operation options, you can configure options besides the requiredReplicaSetName attribute based on your needs, but the default parameters are sufficient in most scenarios.
MongoClientOptions options = MongoClientOptions.builder()
.requiredReplicaSetName("Cluster0-clustername-shard-0").socketTimeout(30000)
.connectionsPerHost(1).sslEnabled(true).build();
return new MongoClient(seedList, credentials, options);
So without sslEnabled(true) I had the same error as before, but with it works fine.
And I also found this issue https://github.com/metabase/metabase/issues/6678 where people had the same problems and as I was able to understand, they spoke about SSL problem too.
In fact here https://docs.atlas.mongodb.com/driver-connection/ - "Connect via Driver" there is Prerequisites:
TLS/SSL
Clients must have support for TLS/SSL to connect to an Atlas cluster.
Clients must have support for the SNI TLS extension to connect to an Atlas M0 Free Tier or M2/M5 shared starter cluster. To verify that your driver supports the SNI TLS extension, refer to the Driver Compatibility matrix for your driver. Drivers which are compatible with MongoDB version 3.4 and up support the SNI TLS extension
I can't do objective conclusions, but may be this error is throws by lack of implementation SSL.
name surname February 5, 2019 at 7:07 PMEdited
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:
return new MongoClient("cluster0-clustername-shard-00-00-raa4n.mongodb.net", 27017);
or this to connect to the cluster of 3 nodes:
ServerAddress seed1 = new ServerAddress("cluster0-clustername-shard-00-00-raa4n.mongodb.net", 27017);
ServerAddress seed2 = new ServerAddress("cluster0-clustername-shard-00-01-raa4n.mongodb.net",27017);
ServerAddress seed3 = new ServerAddress("cluster0-clustername-shard-00-02-raa4n.mongodb.net",27017);
// Construct the Seed list.
List<ServerAddress> seedList = new ArrayList<ServerAddress>();
seedList.add(seed1);
seedList.add(seed2);
seedList.add(seed3);
return new MongoClient(seedList);
But nothing changed, like before I was received the same error:
20:05:54,501 INFO [org.mongodb.driver.cluster] (cluster-ClusterId{value='5c59de917aea61231670d8ee', description='null'}-cluster0-clustername-shard-00-00-raa4n.mongodb.net:27017) Exception in monitor thread while connecting to server cluster0-clustername-shard-00-00-raa4n.mongodb.net:27017: com.mongodb.MongoSocketReadException: Prematurely reached end of stream
Davide D'Alto February 5, 2019 at 5:23 PM
Sure, make sure once you have created the class to change this property:
<property name="hibernate.ogm.datastore.provider" value="mongodb"/>
to something like this:
<property name="hibernate.ogm.datastore.provider" value="org.project.MYCustomMongoDBDatastoreProvider"/>
name surname February 5, 2019 at 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 at 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
Details
Details
Assignee
Reporter
Bug Testcase Reminder (view)
Bug reports should generally be accompanied by a test case!
Bug Testcase Reminder (edit)
Bug reports should generally be accompanied by a test case!
I was asked this question on https://stackoverflow.com/questions/54487413/usage-of-hibernate-ogm-with-cloud-mongodb-atlas-m0-free-tier/54532599#54532599
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
20:02:01,094 INFO [org.mongodb.driver.cluster] (ServerService Thread Pool -- 78) Cluster description not yet available. Waiting for 30000 ms before timing out 20:02:01,175 INFO [org.mongodb.driver.cluster] (cluster-ClusterId{value='5c5497a97aea6111622c7540', description='null'}-cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017) Exception in monitor thread while connecting to server cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017: com.mongodb.MongoSocketReadException: Prematurely reached end of stream at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112) at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:570) at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:441) at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295) 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)
My persistence.xml that causes the error:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd" version="2.2"> <!-- @@@ MongoDB HIBERNATE OGM PERSISTENCE UNIT @@@ --> <persistence-unit name="PersistenceUnitNoSQL" transaction-type="JTA"> <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider> <class>org.companyname.model.UserEntity</class> <class>org.companyname.model.ItemEntity</class> <properties> <property name="hibernate.ogm.datastore.provider" value="mongodb"/> <property name="hibernate.ogm.datastore.host" value="cluster0-clustername-shard-00-00-raa4n.mongodb.net:27017,cluster0-clustername-shard-00-01-raa4n.mongodb.net:27017,cluster0-clustername-shard-00-02-raa4n.mongodb.net:27017"/> <property name="hibernate.ogm.datastore.database" value="databasename"/> <property name="hibernate.ogm.datastore.create_database" value="true"/> <property name="hibernate.ogm.datastore.username" value="atlas-user-name"/> <property name="hibernate.ogm.datastore.password" value="atlas-user-password"/> <property name="hibernate.ogm.mongodb.authentication_mechanism" value="SCRAM_SHA_1"/> </properties> </persistence-unit> </persistence>
As @Davide D'Alto suggested I create Factory to interact for the persistence unit
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenceUnitNoSQL");