Connection with Eclipselink (JPA) to Atlas

I would like to connect to my Atlas cluster using eclipselinks nosql feature. I manage to connect to a local mongodb using this persistance.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
    <persistence-unit name="mongo-test" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <properties>
            <property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>
            <property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
            <property name="eclipselink.nosql.property.mongo.port" value="27017"/>
            <property name="eclipselink.nosql.property.mongo.host" value="localhost"/>
            <property name="eclipselink.nosql.property.mongo.db" value="xrn-testing"/>
            <property name="eclipselink.logging.level" value="FINEST"/>
        </properties>
    </persistence-unit>
</persistence>

However from Atlas I just get a connection string which looks like this:

mongodb+srv://<user>:<password>@test-cluster-0.xxxx.mongodb.net/?retryWrites=true&w=majority

Question: How do I need to set up my persistence.xml to connect to the cluster. In particular:

  • what’s the port? Is it still 27017? Do I need to provide it at all?
  • is test-cluster-0.xxxx.mongodb.net the correct host? What about the query string?
  • how to correctly include user name and password?

Or does anybody have an example persistence.xml for an Atlas cluster?

Greetings,
Marcus

Hi @Marcus_Blumel and welcome to the MongoDB community forum!!

<property name="eclipselink.nosql.property.mongo.port" value="27017"/>
<property name="eclipselink.nosql.property.mongo.host" value="localhost"/>
<property name="eclipselink.nosql.property.mongo.db" value="xrn-testing"/>

The localhost connection you advised was working may be due to the above configuration details (and possibly more). However, this appears to be more of a eclipselink question rather than a MongoDB question. The connection string provided is generally for the official MongoDB drivers, MongoDB Compass or mongosh.

You may wish to raise this in the Eclipse forum or possibly Stack Overflow to see if the software is capable of using the DNS Seed List Connection Format connection string example you provided.

However, to answer your questions:

In saying so, for the port specific question, I would refer to the following Connecting to a Database Deployment documentation which includes the port details for Atlas.

Based off your connection string, the test-cluster-0.xxxx.mongodb.net value is most likely the SRV (not inclusive of the prefix). More details regarding the SRV record within DNS Seed List Connection Format can be detailed on the following topic reply.

Let us know if you have any further questions.

Best Regards
Aasawari