我需要您的建議,代碼審查或改進有關我的多重模式實現。我想要多連接支持MongoDB服務器。Multiton模式使用的副作用
public class MongoDatabaseFactory {
private static volatile Map<String, MongoDatabase> connections = new ConcurrentHashMap<String, MongoDatabase>();
public static MongoDatabase getDatabase(Databases database) throws MongoException {
if (null == database) throw new MongoException("Database not found");
if (null == database.name() || database.name().isEmpty()) throw new MongoException("Database not found");
if (!connections.containsKey(database.name()) || null == connections.get(database.name())) {
synchronized (database) {
if (!connections.containsKey(database.name()) || null == connections.get(database.name())) {
connectDB(database);
}
}
}
if (!connections.get(database.name()).isAuthenticated()) {
synchronized (database) {
if (!connections.get(database.name()).isAuthenticated()) {
connectDB(database);
}
}
}
return connections.get(database.name());
}
}
多子模式的最佳實踐是什麼?
[A MongoClient已經連接池(http://docs.mongodb.org/ecosystem/drivers/java-concurrency/),爲什麼你想/需要增加額外的層? – assylias