Catch "connection refused"

48 views
Skip to first unread message

jday@sbi

unread,
May 8, 2012, 8:09:01 PM5/8/12
to mongodb-user
There was a similar post, but it wasn't resolved and I couldn't add to
it.

I am building an application as a service and I am engineering it to
handle if the database backend closes or becomes unavailable for any
particular reason.

One of the scenarios I have is if the mongodb service is shut down
unexpectedly. At present I can catch most throwable errors, except
for:

May 08, 2012 5:13:42 PM com.mongodb.DBTCPConnector
fetchMaxBsonObjectSize
WARNING: Exception determining maxBSON size using0
java.io.IOException: couldn't connect to [/127.0.0.1:27017]
bc:java.net.ConnectException: Connection refused
at com.mongodb.DBPort._open(DBPort.java:228)
at com.mongodb.DBPort.go(DBPort.java:112)
at com.mongodb.DBPort.go(DBPort.java:93)
at com.mongodb.DBPort.findOne(DBPort.java:146)
at com.mongodb.DBPort.runCommand(DBPort.java:157)
at
com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:
457)
at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:
444)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:209)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:
305)
at com.mongodb.DB.command(DB.java:160)
at com.mongodb.DB.command(DB.java:183)
at com.mongodb.DB.command(DB.java:144)
at com.mongodb.DB._doauth(DB.java:541)
at com.mongodb.DB.authenticate(DB.java:478)
at
com.smartbombinteractive.dataRunners.MongoRawRunner.connect(MongoRawRunner.java:
135)
...

If I include java.io.IOException and attempt to catch (IOException e)
the error still goes through to output and pummels my logs. I have an
(Exception e) catchall at the end of the catches that is being skipped
too.

How do I correctly catch this exception?

Bryan

unread,
May 17, 2012, 3:53:15 PM5/17/12
to mongodb-user
Unfortunately you won't be able to catch that exception since it is
already caught and logged within the DBTCPConnector's
fetchMaxBsonObjectSize method. However if the connection from the
driver to the database was lost, your call to DB.authenticate() should
throw a MongoException which you can catch.

Please feel free to file a bug: (https://jira.mongodb.org/browse/JAVA)
to address the exception handling in fetchMaxBsonObjectSize() and
reduce the noise in our logging.
Reply all
Reply to author
Forward
0 new messages