I am using connection pooling in java. Just want to make sure i am using it correctly.
so, here is my mongoconnection class with getDatabase method.
public class MongoConnection {
private static MongoConnection mongoConnection = null;
public static MongoConnection getInstance() {
if (mongoConnection == null) {
mongoConnection = new MongoConnection();
}
return mongoConnection;
}
private MongoClient mongoClient = null;
private MongoDatabase mongoDatabase = null;
private MongoConnection() {
mongoClient = new MongoClient("localhost");
mongoDatabase = mongoClient.getDatabase("test");
}
public MongoDatabase getDatabase() {
return mongoDatabase;
}
}
and here is code snippet that uses that.
public void insertCustomer(document){
MongoCollection<Document> collection =
MongoConnection.getInstance().getDatabase().getCollection("customers");
collection.insertOne(document);
}
inserCustomer method get called multiple times.
thats all.