gpt4 book ai didi

mongodb - 创建 MongoClient 实例时如何捕获异常

转载 作者:行者123 更新时间:2023-12-03 00:26:48 24 4
gpt4 key购买 nike

我正在使用 Mongo DB java 驱动程序连接到 mongo 实例。下面是我用来创建 MongoClient 实例的代码。

try {
new MongoClient("localhost", 1111);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

如果主机名或端口号不正确,我将收到以下异常。我想知道如何捕获这些异常。 MongoDB 连接发生在内部线程中,客户端代码无法捕获。我想知道 MongoClient 是否已正确连接。我怎样才能得到这些信息?

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

编辑1

上面显示的异常没有被我的代码捕获。它可能被 Mongo 代码捕获。所以我不知道实例是否正确创建。

最佳答案

服务器连接是在守护线程上创建的。长话短说,您将无法在创建 Mongo 客户端时检查与连接相关的错误。

当您创建第一个涉及读取或写入的真实数据库时,您必须延迟连接检查。

仅用于演示目的,以便您获得想法。

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}

来自守护线程的 MongoSocketOpenException

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

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 block 中,它可以正常检查与连接相关的错误。

关于mongodb - 创建 MongoClient 实例时如何捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40813060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com