Spring boot detect mongodb connection error located in another maven module (MongoSocketOpenException)

huangapple go评论153阅读模式
英文:

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: &quot;lydsam&quot;.  
The first module is called: **&quot;bd&quot;**, it is my connection with my database.

    @Configuration
    public class ConnectionBD {

	    @Bean
        public MongoDatabaseFactory mongoDatabaseFactory(){
		    return new SimpleMongoClientDatabaseFactory(&quot;mongodb://localhost:27017/lydsam&quot;);
        }

        @Bean
        public MongoTemplate mongoTemplate() {
            try{
    	        return new MongoTemplate(mongoDatabaseFactory());
            }catch(Exception e){
                System.out.println(&quot;offline with mongodb&quot;);
                System.out.println(e.getMessage());
                return null;
            }
        }
    }
and I have another module called: **&quot;sales&quot;**

    @SpringBootApplication(scanBasePackages = { &quot;org.lydsam.sales&quot;,
				&quot;org.lydsam.bd&quot; })
    public class Sales_aplicacion {

	    public static void main(String[] args) {
		    try {
			    SpringApplication.run(Sales_aplicacion.class, args);
		    } catch (Exception e) {
			    System.out.println(&quot;Error executing in application &#39;Sales&#39;: &quot; + 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 **&quot;try catch&quot;** but it didn&#39;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>



huangapple
  • 本文由 发表于 2020年8月27日 02:37:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63603775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定