Skip to content

Instantly share code, notes, and snippets.

@amgohan
Last active August 29, 2017 20:40
Show Gist options
  • Save amgohan/8252487 to your computer and use it in GitHub Desktop.
Save amgohan/8252487 to your computer and use it in GitHub Desktop.
Mongo Connection Manager using Morphia
import java.net.UnknownHostException;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import com.mongodb.MongoClient;
public class MongoConnectionManager {
private static final MongoConnectionManager INSTANCE = new MongoConnectionManager();
private static final String DB_NAME = "dbtest";
private Morphia morphia = null;
private Datastore datastore = null;
private MongoClient mongoClient = null;
private MongoConnectionManager() {
morphia = new Morphia();
try {
mongoClient = new MongoClient();
} catch (UnknownHostException e) {
e.printStackTrace();
}
datastore = morphia.createDatastore(mongoClient, DB_NAME);
}
public static MongoConnectionManager getInstance() {
return INSTANCE;
}
public Morphia getMorphia() {
return morphia;
}
public void setMorphia(Morphia morphia) {
this.morphia = morphia;
}
public Datastore getDatastore() {
return datastore;
}
public void setDatastore(Datastore datastore) {
this.datastore = datastore;
}
public MongoClient getMongoClient() {
return mongoClient;
}
public void setMongoClient(MongoClient mongoClient) {
this.mongoClient = mongoClient;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment