java "UnknownHostException"

瀏覽次數:2,205 次
跳到第一則未讀訊息

Tim Jones

未讀,
2010年12月21日 下午6:40:112010/12/21
收件者:mongodb-user
Hi,

I'm relatively new to MongoDB. I'm attempting to use the Java driver
to connect to a db hosted on MongoHQ.com. I'm able to connect to this
database with no problems via the Mongo command-line prompt or via the
Mongo PHP driver. However, when attempting to connect through Java,
I'm given an "UnkownHostException" with no further explanation.

The line producing the exception is simple:
~~
m = new Mongo("flame.mongohq.com",27092);
~~

Again, this host name and port number work AOK in other contexts. I
haven't tried to read or update any documents yet, or even to
authenticate; so far I'm only trying to connect. Can anyone advise as
to why I might be receiving this exception and how I could fix it?

I also tried prefixing the hostname with "mongodb://" but that didn't
help.

Many thanks,

=Tim=

Brendan W. McAdams

未讀,
2010年12月21日 晚上8:21:562010/12/21
收件者:mongod...@googlegroups.com
Tim,

Would you mind pasting the entire stacktrace that came with the Unknown Host exception? There are a few possible cases that it could be thrown.

Also - what version of the Java driver are you using?


--
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.


Alvin Richards

未讀,
2010年12月21日 晚上8:25:332010/12/21
收件者:mongodb-user

Nat

未讀,
2010年12月21日 晚上8:39:562010/12/21
收件者:mongodb-user
Tim,

- Did you try those CLI, PHP from the same box as Java driver?
- What platform and version that you run Java Driver from?
- Did you try to do nslookup or ping to flame.mongohq.com from that
box?
- If the problem still persists, could you try to disable DNS caching
in Java by specifying command line option -Dnetworkaddress.cache.ttl=0
to see if the problem goes away or not

Tim Jones

未讀,
2010年12月22日 下午4:44:062010/12/22
收件者:mongodb-user
Thanks for the speedy replies!

Brendan, I'll post a stack-trace shortly. I'm using v2.3 of the java
driver.

Alvin, your suggested syntax produces the same error.

Nat, my CLI and PHP are running from the same 'box' as my Java -- it's
my OSX laptop. I'm running Java in Eclipse.

=Tim=

Scott Hernandez

未讀,
2010年12月22日 下午4:46:192010/12/22
收件者:mongod...@googlegroups.com
Can you post the results from 'netstat -na | grep 27017' ? Is mongod
listening? It could be a firewall issue as the php driver (and shell)
might connect via the unix domain socket.

Tim Jones

未讀,
2010年12月22日 下午5:08:002010/12/22
收件者:mongodb-user
Brendan, the only stack-track I'm able to produce right now is pretty
banal:
~~
java.lang.Error: Unresolved compilation problem:
Unhandled exception type UnknownHostException

at test.init(test.java:10)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:680)
~~

I'm going to mess around with Eclipse and see if I can produce details
about where in the Mongo library the error is being thrown from.

Scott, running that netstat/grep produces an empty result.

=Tim=



Brendan W. McAdams

未讀,
2010年12月22日 下午5:10:072010/12/22
收件者:mongod...@googlegroups.com
You're getting a *COMPILATION* error related to UnknownHostException.

UnknownHostException is a checked exception, which you MUST explicitly catch when you create a new instance of com.mongodb.Mongo, or throw further up.

See:


You need a try/catch block or your outer method must declare throws UnknownHostException



=Tim=



Scott Hernandez

未讀,
2010年12月22日 下午5:14:482010/12/22
收件者:mongod...@googlegroups.com
Independent of your code issues the netstat shows that your aren't
running mongod on the default port (27017)

Here is a sample from my machine.

skot@stump:~/$ netstat -na | grep 27017
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN
unix 2 [ ACC ] STREAM LISTENING 134878414
/tmp/mongodb-27017.sock

Note the two lines, one for the tcp port and one for the unix domain socket.

Brendan W. McAdams

未讀,
2010年12月22日 下午5:15:152010/12/22
收件者:mongod...@googlegroups.com
Sorry - I realized my excitement at going "OH, I see" may have seemed a bit more brash than I intended.


Try replacing:

m = new Mongo("flame.mongohq.com",27092);

With:

try {
  m = new Mongo("flame.mongohq.com",27092);
} catch (UnknownHostException e) {
  System.err.println("Connection failed: " + e);

Tim Jones

未讀,
2010年12月22日 下午5:22:582010/12/22
收件者:mongodb-user
Brendan, the code you suggest produces a new error
"UnknownHostException cannot be resolved to a type". This just looks
like a simple syntax issue though.

Regardless, it seems to me that simply catching the exception isn't
going to solve the underlying problem, which is that the host is not
being found. Catching the exception will allow my program to fail
gracefully... but it's still a fail, right? Unless I'm missing
something.

Scott, I'm not sure I'm following you. Mongod is the db server, right?
My box is the client, not the server/daemon.


Tim Jones

未讀,
2010年12月22日 下午5:29:532010/12/22
收件者:mongodb-user
Ah no, Brendan, I take it all back. I told it to catch "Exception e"
instead of "UnknownHostException e" and it ran AOK.

And so it has been revealed that I am a PHP/Python hacker who is new
to Java. It did not even occur to me that failing to explicitly catch
exceptions when instantiating a class would cause a compilation
error.

Thanks for the education everyone.

=Tim=

Brendan W. McAdams

未讀,
2010年12月22日 下午5:29:572010/12/22
收件者:mongod...@googlegroups.com
Sorry, you'll have to either fully qualify it or import it -- full class & package is  java.net.UnknownHostException

The issue ISNT that the host isn't being found, it's just a confusing compiler message.
java.net.UnknownHostException is a checked exception in Java which means you must either explicitly catch it, or declare that you can throw it in your method.  Your code can't compile because you aren't handling UnknownHostException.  It specifically says "Compilation Error -- Unhandled Exception".  If the code was running and failed to connect it would throw an exception like this:

new com.mongodb.Mongo("omg.ponies")

java.net.UnknownHostException: omg.ponies
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:850)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1201)
at java.net.InetAddress.getAllByName0(InetAddress.java:1154)
at java.net.InetAddress.getAllByName(InetAddress.java:1084)
at java.net.InetAddress.getAllByName(InetAddress.java:1020)
at com.mongodb.ServerAddress._getAddress(ServerAddress.java:176)
at com.mongodb.ServerAddress.<init>(ServerAddress.java:59)
at com.mongodb.ServerAddress.<init>(ServerAddress.java:38)
at com.mongodb.Mongo.<init>(Mongo.java:92)





--

Brendan W. McAdams

未讀,
2010年12月22日 下午5:35:062010/12/22
收件者:mongod...@googlegroups.com
For what it's worth - only *SOME* Exceptions.  Java has two kinds:

java.lang.Exception which MUST be explicitly caught (or your method needs to say 'throws <exception type>')
java.lang.RuntimeException which doesn't require any explicit catching.

If you fail to handle a Exception (not a runtimeexception) Java will fail to compile, because they're declared as happening in the method you called.

Most of the exceptions MongoDB throws are RuntimeExceptions --- they only get caught if you WANT to catch them. (be careful though - uncaught runtime exceptions WILL crash your JVM).

The underlying java networking libraries throw UnknownHostException though which is checked, so you have to handle it.

回覆所有人
回覆作者
轉寄
0 則新訊息