创建MongoClient实例时如何捕获异常 [英] How to catch exception when creating MongoClient instance
问题描述
我正在使用Mongo DB Java驱动程序连接到mongo实例.下面是我用来创建MongoClient实例的代码.
I am using Mongo DB java driver to connect to mongo instance. Below is the code I am using to create the MongoClient instance.
try {
new MongoClient("localhost", 1111);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果主机名或端口号不正确,我将获得以下异常.我徘徊着如何捕捉那些例外. MongoDB连接发生在内部线程中,客户端代码无法捕获该内部线程.我想知道MongoClient是否已正确连接.我如何获得这些信息?
If the hostname or port number is not correct, I will get below exceptions. I wander how I can catch that exceptions. MongoDB connecting is happening in an internal thread which can't be catched by client code. I want to know whether the MongoClient has connected correctly or not. How can I get that information?
INFO: Exception in monitor thread while connecting to server localhost:0
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:116)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Can't assign requested address (connect failed)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 more
EDIT1
我的代码没有捕获上面显示的异常.它可能会被Mongo代码捕获.因此,我不知道该实例是否正确创建.
The exceptions shown above is not catch by my code. It may be catch by Mongo code. So I don't know whether the instance is created correctly or not.
推荐答案
服务器连接是在守护程序线程上创建的.长话短说,创建Mongo Client时您将无法检查与连接有关的错误.
The server connections are created on daemon threads. So long story short you'll not to able to check the connection related errors while creating the Mongo Client.
制作第一个涉及读或写的真实数据库时,必须延迟连接检查.
You'll have to delay your connection check when you make your first real database which involves a read or write.
仅出于演示目的,您可以了解自己的想法.
Just for demonstration purposes for you to get an idea.
MongoClient mongoClient = new MongoClient("127.0.34.1", 89);
DB db = mongoClient.getDB("test");
try {
db.addUser("user", new char[] {'p', 'a', 's', 's'});
} catch(Exception e) { MongoTimeoutException exception}
来自Deamon线程的MongoSocketOpenException
MongoSocketOpenException from Deamon Thread
INFO: Exception in monitor thread while connecting to server 127.0.34.1:89
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:116)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: connect
主线程出现MongoTimeoutException
MongoTimeoutException from Main Thread
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.34.1:89, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket},
caused by {java.net.ConnectException: Connection refused: connect}}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:375)
因此用MongoTimeoutException
将代码包装在try catch块中,它将可以正常工作以检查与连接有关的错误.
So wrap code in try catch block with MongoTimeoutException
and it will work okay for checking connection related errors.
这篇关于创建MongoClient实例时如何捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!