Java MongoDB单例

package com.actionsoft.bpms.org.event;

import org.bson.Document;

import com.mongodb.MongoClient;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;

public class MongodbUtil {

private static  volatile MongodbUtil mg ;

private  MongoCollection collection;

private MongodbUtil(){}

public static MongodbUtil getInstance() {

        if (mg == null) {

            synchronized (MongodbUtil.class) {

                if (mg == null) {

                  mg=new MongodbUtil();

                  MongoClient mongoClient = new MongoClient( "192.168.1.47" , 27017 );

          MongoDatabase mongoDatabase = mongoClient.getDatabase("hool"); 

          System.out.println("Connect to database successfully");

          MongoCollection collection = mongoDatabase.getCollection("test");

          mg.setCollection(collection);

                }

            }

        }

        return mg;

    }

public MongoCollection getCollection() {

return collection;

}

public void setCollection(MongoCollection collection) {

this.collection = collection;

}

}

你可能感兴趣的:(Java MongoDB单例)