2013-07-09 39 views
0

我有問題配置文件inb mongoDB

我正在使用jelastic來託管java和mongoDB應用程序。我的應用程序和他們提供的mongoDB之間的連接有問題。

他們的配置文件是這樣的:


公共類MongoManager {

static String host, dbname, user, password; 

public void addData(int repeats) { 
    try { 
     DBCollection dbc = null; 

     Properties prop = new Properties(); 
     prop.load(new FileInputStream(System.getProperty("user.home") + "/mydb.cfg")); 

     host = prop.getProperty("host").toString(); 
     dbname = prop.getProperty("dbname").toString(); 
     user = prop.getProperty("user").toString(); 
     password = prop.getProperty("password").toString(); 

     System.out.println("host: " + host + "\ndbname: " + dbname + "\nuser: " + user + "\npassword: " + password); 

     Mongo m = new Mongo(host, 27017); 
     DB db = m.getDB(dbname); 

     if (db.authenticate(user, password.toCharArray())) { 
      System.out.println("Connected!"); 
     } else { 
      System.out.println("Connection failed"); 
     } 

     try { 
      db.getCollection("mycollection"); 
     } catch (Exception e) { 
      db.createCollection("mycollection", null); 
     } finally { 
      System.out.println("Repeats: " + repeats); 
      for (int i = 1; i <= repeats; i++) { 
       BasicDBObject data = new BasicDBObject("data", new Date()); 
       db.getCollection("mycollection").save(data); 
       System.out.println("INFO: row added " + data); 
      } 
     } 
    } catch (IOException ex) { 
    } 
} 

和礦山


公共類MongodbUtil {

private static Mongo mongo = null; 
private static Morphia morphia = null; 
private static Datastore ds = null; 
private MongodbUtil() {}; 

public static synchronized DB getDB(String str) throws Exception { 
    if(mongo == null) { 
     mongo = new Mongo(); 
    } 
    return mongo.getDB(str); 
} 
public static synchronized Mongo getMongo() throws Exception { 
    if(mongo == null) { 
     mongo = new Mongo("localhost", 27017); 
    } 
    return mongo; 
} 
public static synchronized Morphia getMorphia() throws Exception { 
    if(morphia == null) { 
     mongo = getMongo(); 
     morphia = new Morphia(); 
     morphia.mapPackage("com.sogeti.simulator.entity"); 
    } 

    return morphia; 
} 
public static synchronized Datastore getDataStore(){ 
    if(ds == null){ 
     try { 
      morphia = getMorphia(); 
      ds = morphia.createDatastore(mongo, "Simulator"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    return ds; 
} 

是否是相同的文件? 我怎樣才能把我的配置文件屬性,如主機?密碼?和別的 ?!

我的MongoDB.cfg.xml看起來像這樣,但我認爲這是不好的,因爲我不使用SPRING或MAVEN: 我沒有看到任何簡單的MongoDB.cfg.xml在web中的例子。

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd;「>

<dependency> 
    <groupId>com.morphia.Morphia</groupId> 
    <artifactId>morphia</artifactId> 
    <version>0.99</version> 
</dependency> 

<dependency> 
    <groupId>com.mongodb.Mongo</groupId> 
    <artifactId>mongo</artifactId> 
    <version>2.10.1</version> 
</dependency> 

回答

0

在我看來,類似的問題在問Jelastic社區here。 該問題已解決,請注意該主題中提出的建議。