如何解决多线程面试问题下Pymongo连接过多的问题

博客分类:
最近项目用到 MongoDB , 主要是一些读取数据及改状态位的操作. 因为是结合了最近流行的 Storm进行大数据的分析处理,并将分析结果插入Vertica数据库,所以在多线程高并发的情境下, 会发现 Vertica 数据库中有部分重复的数据. 这到底是什么原因导致的呢?笔者开始也是一筹莫 展,重复去看 MongoDB 的 API , 终于有了新发现 :
com.mongodb.DB 这个类有三个很重要的方法 :
public abstract void requestStart() :
starts a new "consistent request". Following this call and until requestDone() is called, all db operations should use the same underlying connection. This is useful to ensure that operations happen in a certain order with predictable results.
public abstract void requestDone():
ends the current "consistent request"
public abstract void requestEnsureConnection():
ensure that a connection is assigned to the current "consistent request" (from primary pool, if connected to a replica set)
具体再查了 MongoDB相关的文档才发现, mongodb 的java driver是不支持是事务性的,所以如果想保证在多线程高并发的情境下不出错(指定的一批数据在同一个底层的数据连接中完成),就必须在使用DB的时候加上上述三个方法。
按照 API 提供的方法说明,在项目代码获得DB的后面加上db.requestStart(),db.requestEnsureConnection(),然后在执行完数据库操作之后加上db.requestDone(),成功解决了数据重复的问题!
原创文章,转载请注明出处:
BigCat2013
浏览: 20781 次
来自: 上海
string2020 写道上面写的使用场景太抽象了,求其他的使 ...
上面写的使用场景太抽象了,求其他的使用场景
string2020 写道集群搭建好之后,有什么用搭建一些分布 ...
集群搭建好之后,有什么用
devilyard 写道原来挺简单的万事开头难
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'2013年 总版技术专家分年内排行榜第三
2012年 总版技术专家分年内排行榜第七
2016年10月优秀大版主2016年8月优秀大版主
2016年9月 总版技术专家分月排行榜第二
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。代码-MongoDB 线程长连接问题
作者:用户
浏览:321 次
MongoDB线程长连接问题我在使用MongoDB的过程当中,遇到如下问题:我的MongoDB一直存在4个ESTABLISHED连接,一直未释放,即使任务已经执行完了。代码如下,其中Client只会在
MongoDB 线程长连接问题
我在使用MongoDB的过程当中,遇到如下问题:
我的MongoDB一直存在4个ESTABLISHED 连接,一直未释放,即使任务已经执行完了。
代码如下,其中Client只会在系统启动的时候初始化一次,当然在系统停止contextDestroyed的时候Client也调用了close:
public class ExportServer extends BasicMongoServer {
public final static Logger LOG = Logger.getLogger(ExportServer.class);
public static MongoClient mongoC
private static DB exportDB;
private static DBCollection exportC
初始化MongoDB Client
public static void init(String add,String username,String password) throws UnknownHostException {
ArrayList&ServerAddress& addr = new ArrayList&ServerAddress&();
for (String s : add.split(",")) {
addr.add(new ServerAddress(s.trim()));
if (addr.size() & 1) {
mongoClient = new MongoClient(addr, getOptions());
mongoClient = new MongoClient(add, getOptions());
mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
exportDB = mongoClient.getDB("export");
exportDB.authenticate(username, password.toCharArray());
exportCollection = exportDB.getCollection("export");
if (exportCollection.getIndexInfo().size() == 0) {
("adding necessary indexes on export history collection");
BasicDBObject compoundIndex = new BasicDBObject();
compoundIndex.put("status", 1);
exportCollection.ensureIndex(compoundIndex);
compoundIndex.clear();
compoundIndex.put("key", 1);
exportCollection.ensureIndex(compoundIndex);
compoundIndex.clear();
compoundIndex.put("uid", 1);
exportCollection.ensureIndex(compoundIndex);
更新document
public static void update(DBObject query, DBObject update) {
exportCollection.update(query, update);
保存docuemnt
public static void save(DBObject obj) {
exportCollection.save(obj);
这些代码当中也没有什么特别操作,包括在应用停止的时候我也会将Client关闭
请问各位是否有任何建议或者解决思路。
用netstat -nlp等,查看一下对应的进程,然后就是对应的代码多看看是否有其他分支退出了,从而没有释放连接
【云栖快讯】红轴机械键盘、无线鼠标等753个大奖,先到先得,云栖社区首届博主招募大赛9月21日-11月20日限时开启,为你再添一个高端技术交流场所&&
稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一
6款热门基础云产品6个月免费体验;2款产品1年体验;1款产品2年体验
弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率
开发者常用软件,超百款实用软件一站式提供

我要回帖

更多关于 java多线程问题 的文章

 

随机推荐