英文:
Spring boot detect mongodb connection error located in another maven module (MongoSocketOpenException)
问题
我有一个名为 "lydsam" 的 Java 项目。
第一个模块名为 "bd",它是我与数据库的连接。
@Configuration
public class ConnectionBD {
@Bean
public MongoDatabaseFactory mongoDatabaseFactory(){
return new SimpleMongoClientDatabaseFactory("mongodb://localhost:27017/lydsam");
}
@Bean
public MongoTemplate mongoTemplate() {
try{
return new MongoTemplate(mongoDatabaseFactory());
}catch(Exception e){
System.out.println("offline with mongodb");
System.out.println(e.getMessage());
return null;
}
}
}
我还有另一个名为 "sales" 的模块。
@SpringBootApplication(scanBasePackages = { "org.lydsam.sales",
"org.lydsam.bd" })
public class Sales_aplicacion {
public static void main(String[] args) {
try {
SpringApplication.run(Sales_aplicacion.class, args);
} catch (Exception e) {
System.out.println("在应用程序 'Sales' 中执行时出错:" + e.getMessage());
}
}
}
当我运行应用程序时一切正常,我想在断开数据库连接时捕获错误,然后在控制台显示带有消息的错误。
我尝试过使用 "try catch",但它对我不起作用,并且我收到以下错误。
com.mongodb.MongoSocketOpenException: 打开套接字时出现异常
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-4.0.3.jar:na]
at java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]
Caused by: java.net.ConnectException: 拒绝连接:没有更多信息
at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:579) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:339) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:603) ~[na:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:63) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.0.3.jar:na]
... 3 common frames omitted
请帮助我。
<details>
<summary>英文:</summary>
I have a project in java called: "lydsam".
The first module is called: **"bd"**, it is my connection with my database.
@Configuration
public class ConnectionBD {
@Bean
public MongoDatabaseFactory mongoDatabaseFactory(){
return new SimpleMongoClientDatabaseFactory("mongodb://localhost:27017/lydsam");
}
@Bean
public MongoTemplate mongoTemplate() {
try{
return new MongoTemplate(mongoDatabaseFactory());
}catch(Exception e){
System.out.println("offline with mongodb");
System.out.println(e.getMessage());
return null;
}
}
}
and I have another module called: **"sales"**
@SpringBootApplication(scanBasePackages = { "org.lydsam.sales",
"org.lydsam.bd" })
public class Sales_aplicacion {
public static void main(String[] args) {
try {
SpringApplication.run(Sales_aplicacion.class, args);
} catch (Exception e) {
System.out.println("Error executing in application 'Sales': " + e.getMessage());
}
}
}
Everything works fine when I run the application, **I would like to catch the error when I disconnect my database and then show it to the console with a message.**
I tried using **"try catch"** but it didn't work for me and I get the following error.
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-4.0.3.jar:na]
at java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]
Caused by: java.net.ConnectException: Connection refused: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:579) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:339) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:603) ~[na:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:63) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.0.3.jar:na]
... 3 common frames omitted
Please I need help.
</details>
# 答案1
**得分**: 2
用 try catch 包装你的代码以捕获 MongoTimeoutException 异常。你可以在我的另一个[回答][1]中找到更多详细信息。
[1]: https://stackoverflow.com/questions/40813060/how-to-catch-exception-when-creating-mongoclient-instance/40814641#40814641
<details>
<summary>英文:</summary>
Wrap your code with try catch for MongoTimeoutException. You can find more details in my other [answer][1]
[1]: https://stackoverflow.com/questions/40813060/how-to-catch-exception-when-creating-mongoclient-instance/40814641#40814641
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论