mongodb java连接池_mongodb数据库连接池(java版)

packagestorm.db;importjava.net.UnknownHostException;importjava.util.ArrayList;importcom.mongodb.BasicDBObject;importcom.mongodb.DB;importcom.mongodb.DBCollection;importcom.mongodb.DBCursor;importcom.mongodb.DBObject;importcom.mongodb.MongoClient;importcom.mongodb.MongoClientOptions;importcom.mongodb.ServerAddress;importcom.mongodb.WriteResult;/** mongodb数据库链接池*/

public class MongoDBDaoImpl implementsMongoDBDao

{private MongoClient mongoClient = null;private static final MongoDBDaoImpl mongoDBDaoImpl = new MongoDBDaoImpl();//饿汉式单例模式

privateMongoDBDaoImpl()

{if (mongoClient == null)

{

MongoClientOptions.Builder buide= newMongoClientOptions.Builder();

buide.connectionsPerHost(100);//与目标数据库可以建立的最大链接数

buide.connectTimeout(1000 * 60 * 20);//与数据库建立链接的超时时间

buide.maxWaitTime(100 * 60 * 5);//一个线程成功获取到一个可用数据库之前的最大等待时间

buide.threadsAllowedToBlockForConnectionMultiplier(100);

buide.maxConnectionIdleTime(0);

buide.maxConnectionLifeTime(0);

buide.socketTimeout(0);

buide.socketKeepAlive(true);

MongoClientOptions myOptions=buide.build();try{

mongoClient= new MongoClient(new ServerAddress("127.0.0.1", 27017), myOptions);

}catch(UnknownHostException e)

{

e.printStackTrace();

}

}

}public staticMongoDBDaoImpl getMongoDBDaoImpl()

{returnmongoDBDaoImpl;

}

@OverridepublicDB getDb(String dbName)

{returnmongoClient.getDB(dbName);

}

@OverridepublicDBCollection getCollection(String dbName, String collectionName)

{

DB db=mongoClient.getDB(dbName);returndb.getCollection(collectionName);

}

@Overridepublic booleaninSert(String dbName, String collectionName, String keys, Object values)

{

DB db=mongoClient.getDB(dbName);

DBCollection dbCollection=db.getCollection(collectionName);long num =dbCollection.count();

BasicDBObject doc= newBasicDBObject();

doc.put(keys, values);

dbCollection.insert(doc);if (dbCollection.count() - num > 0)

{

System.out.println("添加数据成功!!!");return true;

}return false;

}

@Overridepublic booleandelete(String dbName, String collectionName, String keys, Object values)

{

WriteResult writeResult= null;

DB db=mongoClient.getDB(dbName);

DBCollection dbCollection=db.getCollection(collectionName);

BasicDBObject doc= newBasicDBObject();

doc.put(keys, values);

writeResult=dbCollection.remove(doc);if (writeResult.getN() > 0)

{

System.out.println("删除数据成功!!!!");return true;

}return false;

}

@Overridepublic ArrayList find(String dbName, String collectionName, intnum)

{int count=num;

ArrayList list = new ArrayList();

DB db=mongoClient.getDB(dbName);

DBCollection dbCollection=db.getCollection(collectionName);

DBCursor dbCursor=dbCollection.find();if (num == -1)

{while(dbCursor.hasNext())

{

list.add(dbCursor.next());

}

}else{while(dbCursor.hasNext())

{if(count==0) break;

list.add(dbCursor.next());

count--;

}

}returnlist;

}

@Overridepublic booleanupdate(String dbName, String collectionName, DBObject oldValue, DBObject newValue)

{

WriteResult writeResult= null;

DB db=mongoClient.getDB(dbName);

DBCollection dbCollection=db.getCollection(collectionName);

writeResult=dbCollection.update(oldValue, newValue);if (writeResult.getN() > 0)

{

System.out.println("数据更新成功");return true;

}return false;

}

@Overridepublic booleanisExit(String dbName, String collectionName, String key, Object value)

{

DB db=mongoClient.getDB(dbName);

DBCollection dbCollection=db.getCollection(collectionName);

BasicDBObject doc= newBasicDBObject();

doc.put(key, value);if (dbCollection.count(doc) > 0)

{return true;

}return false;

}public static voidmain(String args[])

{

MongoDBDaoImpl mongoDBDaoImpl=MongoDBDaoImpl.getMongoDBDaoImpl();

ArrayList list=new ArrayList();

list=mongoDBDaoImpl.find("jd", "phone",-1);

System.out.println(list.size());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值