JAVA????MongoDB??????
???????????? ???????[ 2015/6/15 13:30:14 ] ????????Java ???????
??????????MongoDB?????Щ???ò??????????????????Щ???ò??????????????????У??????????????á?
???????Spring Data??Morphia??????????????????????????????????JDBC?????????????????????????????????????????????
???????????£?
package utils;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientOptions.Builder;
import com.mongodb.WriteConcern;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;
/**
* MongoDB?????? Mongo??????????????????????????????????????У????Mongo???????????????????<br>
* ???Mongo???????????????????????????? <br>
* ???????????? ?? MongoDB??Java???????????????????????????????Mongo????????<br>
* Mongo?и????????????????10???? ?????д???д?????????У????????????Session?????????DB???<br>
* DB??DBCollection?????????<br>
*
* @author zhoulingfei
* @date 2015-5-29 ????11:49:49
* @version 0.0.0
* @Copyright (c)1997-2015 NavInfo Co.Ltd. All Rights Reserved.
*/
public enum MongoDBUtil {
/**
* ???????????????????????????????
*/
instance;
private MongoClient mongoClient;
static {
System.out.println("===============MongoDBUtil?????========================");
CompositeConfiguration config = new CompositeConfiguration();
try {
config.addConfiguration(new PropertiesConfiguration("mongodb.properties"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
// ??????????л???????
String ip = config.getString("host");
int port = config.getInt("port");
instance.mongoClient = new MongoClient(ip?? port);
// or?? to connect to a replica set?? with auto-discovery of the primary?? supply a seed list of members
// List<ServerAddress> listHost = Arrays.asList(new ServerAddress("localhost"?? 27017)??new ServerAddress("localhost"?? 27018));
// instance.mongoClient = new MongoClient(listHost);
// ??????????mongodb???????????£????????mongodb???????????????????????????????????
// boolean auth = db.authenticate(myUserName?? myPassword);
Builder options = new MongoClientOptions.Builder();
// options.autoConnectRetry(true);// ???????true
// options.maxAutoConnectRetryTime(10); // the maximum auto connect retry time
options.connectionsPerHost(300);// ??????????300???????????100
options.connectTimeout(15000);// ???????????>3000????
options.maxWaitTime(5000); //
options.socketTimeout(0);// ??????????0??????
options.threadsAllowedToBlockForConnectionMultiplier(5000);// ????????????????????????????л????“Out of semaphores to get db”????
options.writeConcern(WriteConcern.SAFE);//
options.build();
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11