Java MongoDAO 初始化

GerD82的头像 GerD82 -1 2016-04-13 07:34 0

 基本信息

× 1    × 1    × 1   

浏览数: 7249

分享时间: 11 个月 前

3

[Java]代码    

private DB getMongoDB() {
		try {
			if (MONGODB_DB == null) {
				Mongo mgo = new Mongo(ip, port);
				DB db = mgo.getDB(mongodbName);
				char[] pwd = password.toCharArray();
				db.authenticate(user, pwd);


				MongoOptions options = mgo.getMongoOptions();
				options.socketTimeout = 2000;
				options.autoConnectRetry = true;
				options.socketKeepAlive = true;


				db.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
				MONGODB_DB = db;

				return db;
			} else {
				return MONGODB_DB;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	

您的评论: