Connection MongoDB in cloud (persistance xml)

149 views
Skip to first unread message

Kacper Benedykciuk

unread,
Nov 12, 2014, 6:44:25 AM11/12/14
to mongod...@googlegroups.com
Hi, there!

I have problem with connecting to a Mongo database in the cloud. Local connecting is easy but the second is not. The point of connection for file persistance.xml. In Java code connection with local and database in the cloud is easy. I do this like this:


Almost forgot! I wants to connect to the Mongo database(in cloud) on server mongolab.com.

public static DBCollection getConnection(String dbName, String collectionName) throws UnknownHostException {
        String textUri = "mongodb://login:password@ds039950.mongolab.com:39950/mydatabase";
        MongoClientURI uri = new MongoClientURI(textUri);
        MongoClient mongoClient = new MongoClient(uri);
        //or local only MongoClient mongoClient = new MongoClient("localhost", 27017); 
        DB db = mongoClient.getDB(dbName);
        DBCollection collection = db.getCollection(collectionName);
        return collection;
}

If You prefer You can find this code here:   http://wklej.org/hash/b0cfedbd617/ 


But that's not the point. I want to connect to the file persistance.xml. Since I use the @Entity and @NoSql(dataFormat=DataFormatType.MAPPED) in my class.

So I use to connect with my Mongo database a persistance.xml . But I can only connect to the my local MongoDB.. I don't know how i can edit my persistance.xml to have correct connection with MongoDB in cloud ( on mongolab.com ) . I tried, but was unsuccessful. 


My file persistance.xml looks like ( when i connect with my local MongoDB) : 

app.pharmacy.Drug  <- it is my Entity Class,

This is code:

<?xml version="1.0" encoding="UTF-8"?>
  <persistence-unit name="aptekaPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>app.pharmacy.Drug</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <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="apteka"/>
    </properties>
  </persistence-unit>
</persistence>


If You prefer You can find this code here:  http://wklej.org/hash/895c425ce18/


Maybe Someone can help me?  
Very please with help it because it's part of my Thesis (licenciate).

I hope my Subject is good and place where  add my question too. This is my first post here, so if I did something wrong I apologize.
Thanks!
(Sory i must delete my last post, and public it again with amendments. )

Kacper Benedykciuk

unread,
Nov 17, 2014, 7:55:21 AM11/17/14
to mongod...@googlegroups.com
I resolv this problem! If it helps someone, here is the solution:


<?xml version="1.0" encoding="UTF-8"?>
  <persistence-unit name="aptekaPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>app.pharmacy.Drug</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <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="39950"/>
      <property name="eclipselink.nosql.property.mongo.host" value="ds039950.mongolab.com"/>
      <property name="eclipselink.nosql.property.mongo.db" value="dbname"/>
      
            <property name="javax.persistence.jdbc.password" value="password"/>
            <property name="javax.persistence.jdbc.user" value="login"/>

            <property name="eclipselink.logging.level" value="FINE" />
            <property name="eclipselink.logging.thread" value="true" />
            <property name="eclipselink.logging.session" value="false" />
            <property name="eclipselink.logging.exceptions" value="true" />
            <property name="eclipselink.logging.timestamp" value="false"/>  
    </properties>
  </persistence-unit>
</persistence>

:)
Reply all
Reply to author
Forward
0 new messages