how to catch connection refused exception ?

168 views
Skip to first unread message

go canal

unread,
Sep 24, 2011, 11:21:14 PM9/24/11
to mongod...@googlegroups.com
hi,
would like to catch connection exception, for example, if mongo is not started but we want to open a database,
I saw the exception:
java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect

but not able to catch it. 

try {
mongo = new Mongo (mongo_host, Integer.parseInt(mongo_port));
// get a lis of databases
List<String> dbs = mongo.getDatabaseNames();
} catch (NumberFormatException e) {
logger.error("port is not a valid number");
System.exit(1);
} catch (UnknownHostException e) {
logger.error(e.getMessage());
System.exit(1);
} catch (MongoException.Network e) {
logger.error(e.getMessage());
System.exit(1);

the exception happened when trying to get the list of databases. But it is not captured by the exceptions I used. So wondering which one I should add?
 
thanks,
canal

Scott Hernandez

unread,
Sep 25, 2011, 1:31:50 AM9/25/11
to mongod...@googlegroups.com
java.io.IOException

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.
>

go canal

unread,
Sep 26, 2011, 2:10:48 AM9/26/11
to mongod...@googlegroups.com
no, it does not catch the exception, I have no idea why.
 
thanks,
canal

From: Scott Hernandez <scotthe...@gmail.com>
To: mongod...@googlegroups.com
Sent: Sunday, September 25, 2011 1:31 PM
Subject: Re: [mongodb-user] how to catch connection refused exception ?

> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.
>

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user+unsub...@googlegroups.com.

go canal

unread,
Sep 26, 2011, 2:53:36 AM9/26/11
to mongod...@googlegroups.com
traced the Java driver source code, the error is thrown by fetchMaxBsonObjectSize() in the DBTCPConnector.java


    int fetchMaxBsonObjectSize() {
        if (_masterPortPool == null)
            return 0;
        DBPort port = _masterPortPool.get();
        try {
            CommandResult res = port.runCommand(_mongo.getDB("admin"), new BasicDBObject("isMaster", 1));
            // max size was added in 1.8
            if (res.containsField("maxBsonObjectSize")) {
                maxBsonObjectSize = ((Integer) res.get("maxBsonObjectSize")).intValue();
            } else {
                maxBsonObjectSize = Bytes.MAX_OBJECT_SIZE;
            }
        } catch (Exception e) {
            _logger.log(Level.WARNING, "Exception determining maxBSON size using"+maxBsonObjectSize, e);
        } finally {
            port.getPool().done(port);
        }
        return maxBsonObjectSize;
    }

later it throw MongoException.Network
 
thanks,
canal

From: go canal <goc...@yahoo.com>
To: "mongod...@googlegroups.com" <mongod...@googlegroups.com>
Sent: Monday, September 26, 2011 2:10 PM
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages