我需要您对我的 multiton 模式实现的建议、代码审查或改进。我想为 mongodb 服务器提供多连接支持。
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());
}
}
多吨模式的最佳实践是什么?