Vertx + MongoClient Singleton

时间:2016-02-09 07:13:15

标签: mongodb vert.x

我已经开始使用Vertex并出现了下一个问题。应用模式“Singleton”来处理MongoClient对象是否正确?

这样的事情:

class Mongo {
    private static volatile MongoClient mongoclient = null;

    private Mongo(){};

    public static MongoClient getInstance() {
        if (mongoclient == null) {
            synchronized(Mongo.class) {
                if (mongoclient == null)
                    mongoclient = MongoClient.createShared(vertx, config);
            }
        }
        return mongoclient;
    }
}

0 个答案:

没有答案