由于套接字异常,mongodb插入失败 [英] mongodb insert fails due to socket exception

查看:79
本文介绍了由于套接字异常,mongodb插入失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Eclipse中从事Java项目.我有一个登台服务器和一个实时服务器.这两个人还拥有自己的mongodb,它们在两个不同端口(29017和27017)上的不同服务器上运行. 我想通过Junit测试将数据从实时mongo复制到devel mongo.

I am working on a Java project in Eclipse. I have a staging server and a live server. Those two also have their own mongodbs, which run on a different server on two different ports (29017 and 27017). Via a Junit Test I want to copy data from the live mongo to the devel mongo.

最奇怪的事情:有时它可以工作,有时我会收到套接字错误.我想知道为什么mongo有时会完全拒绝写插入内容,而在其他日子它会完美地工作.这是mongo日志文件(插入代码的地方)和Junit测试脚本的摘录:

Weirdest thing: sometimes it works and sometimes I get a socket error. I wonder why mongo sometimes completely refuses to write inserts and on other days it works flawlessly. Here is an excerpt of the mongo log file (the one where code gets inserted) and the Junit test script:

mongo日志:

Thu Mar 14 21:01:04 [initandlisten] connection accepted from xx.xxx.xxx.183:60848 #1    (1 connection now open)
Thu Mar 14 21:01:04 [conn1] run command admin.$cmd { isMaster: 1 }
Thu Mar 14 21:01:04 [conn1] command admin.$cmd command: { isMaster: 1 } ntoreturn:1 keyUpdates:0  reslen:90 0ms
Thu Mar 14 21:01:04 [conn1] opening db:  repgain
Thu Mar 14 21:01:04 [conn1] query repgain.editorconfigs query: { $and: [ { customer: "nokia" }, { category: "restaurant" } ] } ntoreturn:0 keyUpdates:0 locks(micros) W:5302 r:176 nreturned:0 reslen:20 0ms
Thu Mar 14 21:01:04 [conn1] Socket recv() errno:104 Connection reset by peer xx.xxx.xxx.183:60848
Thu Mar 14 21:01:04 [conn1] SocketException: remote: xx.xxx.xxx.183:60848 error: 9001 socket exception [1] server [xx.xxx.xxx.183:60848]
Thu Mar 14 21:01:04 [conn1] end connection xx.xxx.xxx.183:60848 (0 connections now open)

junit测试脚本:

junit test script:

public class CopyEditorConfig {

protected final Log logger = LogFactory.getLog(getClass());

private static final String CUSTOMER = "customerx";
private static final String CATEGORY = "categoryx";

@Test
public void test() {
    try {

        ObjectMapper om = new ObjectMapper();

        // script copies the config from m2 to m1.

        Mongo m1 = new Mongo("xxx.xxx.com", 29017); // devel
        Mongo m2 = new Mongo("yyy.yyy.com", 27017); // live
        Assert.assertNotNull(m1);
        Assert.assertNotNull(m2);

        logger.info("try to connect to db \"dbname\"");

        DB db2 = m2.getDB("dbname");

        logger.info("get collection \"config\"");
        DBCollection c2 = db2.getCollection("config");
        JacksonDBCollection<EditorTabConfig, ObjectId> ec2 = JacksonDBCollection.wrap(c2, EditorTabConfig.class, ObjectId.class);

        logger.info("find entry with customer {" + CUSTOMER + "} and category {" + CATEGORY + "}");
        EditorTabConfig config2 = ec2.findOne(DBQuery.and(DBQuery.is("customer", CUSTOMER), DBQuery.is("category", CATEGORY)));

        // config
        if (config2 == null) {
            logger.info("no customer found to copy.");
        } else {
            logger.info("Found config with id: {" + config2.objectId + "}");
            config2.objectId = null;
            logger.info("copy config");
            boolean found = false;

            DB db1 = m1.getDB("dbname");

            DBCollection c1 = db1.getCollection("config");
            JacksonDBCollection<EditorTabConfig, ObjectId> ec1 = JacksonDBCollection.wrap(c1, EditorTabConfig.class, ObjectId.class);

            EditorTabConfig config1 = ec1.findOne(DBQuery.and(DBQuery.is("customer", CUSTOMER), DBQuery.is("category", CATEGORY)));

            if (config1 != null) {
                found = true;
            }

            if (found == false) {
                WriteResult<EditorTabConfig, ObjectId> result = ec1.insert(config2);
                ObjectId id = result.getSavedId();
                logger.info("INSERT config with id: " + id);
            } else {
                logger.info("UPDATE config with id: " + config1.objectId);
                ec1.updateById(config1.objectId, config2);
            }

            StringWriter sw = new StringWriter();
            om.writeValue(sw, config2);
            logger.info(sw);
        }

    } catch (Exception e) {
        logger.error("exception occured: ", e);
    }
}
}

当我在eclipse中读取日志时,运行此脚本似乎很成功.我同时获得了c1c2id,数据也位于此处.日志甚至指出,它在devel上找不到配置,然后将其插入.如果我手动将其放在那也是如此.然后它被更新".但是mongo日志保持不变. 发生套接字异常,并且永远不会将数据写入数据库.

Running this script seems like a success when I read the log in eclipse. I get an id for both c1 and c2 and the data is also here. The log even states, that it didn't find the config on devel and inserts it. That also is true, if I put it there manually. It gets "updated" then. But the mongo log stays the same. The socket exception occurs, and the data is never written to the db.

我没有什么好主意来调试它.如果可以的话,我很高兴获得一些提示,说明如何从这里开始.另外,如果缺少任何信息,请告诉我,我很乐意分享.

I am out of good ideas to debug this. If you could, I'd be glad to get some tips how to go from here. Also, if any information is missing, please tell me, I'd be glad to share.

关于, 亚历克斯

推荐答案

注释掉mongod.conf中将IP绑定到127.0.0.1的行. 通常,默认情况下将其设置为127.0.0.1. 对于Linux,此配置文件位置应为/etc/mongod.conf.一旦您将其注释掉,它将接收所有接口的连接.这对我来说也解决了,因为我也收到了这些套接字异常.

Comment out the line in your mongod.conf that binds the IP to 127.0.0.1. Usually, it is set to 127.0.0.1 by default. For Linux, this config file location should be be /etc/mongod.conf. Once you comment that out , it will receive connections from all interfaces. This fixed it for me as i was getting these socket exceptions as well.

这篇关于由于套接字异常,mongodb插入失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆