My Spring boot application uses MongoDB spring boot starter
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
My JPA repository class extends org.springframework.data.mongodb.repository.MongoRepository and my application class uses this annotation - @EnableMongoRepositories. I am using a serverless MongoDB uri like mongodb srv://:@xyz.abc.mongodb.net/xyz
The problem is when I connect my Windows 10 laptop Wi-Fi to a broadband internet connection, I get a "java.net.BindException: Cannot assign requested address: connect" (stack trace below) on application start-up whereas it works perfectly when I connect my Wi-Fi to my mobile hotspot. How can I find out the conflicting port the mongo dB client is using?
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.io.UncheckedIOException: java.net.BindException: Cannot assign requested address: connect
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.18.jar:5.3.18]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.18.jar:5.3.18]
... 85 common frames omitted
Caused by: java.io.UncheckedIOException: java.net.BindException: Cannot assign requested address: connect
at java.base/sun.nio.ch.DatagramSocketAdaptor.connect(DatagramSocketAdaptor.java:120) ~[na:na]
at java.base/java.net.DatagramSocket.connect(DatagramSocket.java:341) ~[na:na]
at jdk.naming.dns/com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:408) ~[jdk.naming.dns:na]
at jdk.naming.dns/com.sun.jndi.dns.DnsClient.query(DnsClient.java:214) ~[jdk.naming.dns:na]
at jdk.naming.dns/com.sun.jndi.dns.Resolver.query(Resolver.java:81) ~[jdk.naming.dns:na]
at jdk.naming.dns/com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434) ~[jdk.naming.dns:na]
at java.naming/com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235) ~[na:na]
at java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141) ~[na:na]
at java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129) ~[na:na]
at java.naming/javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:171) ~[na:na]
at com.mongodb.internal.dns.DefaultDnsResolver.resolveAdditionalQueryParametersFromTxtRecords(DefaultDnsResolver.java:114) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.ConnectionString.<init>(ConnectionString.java:388) ~[mongodb-driver-core-4.4.2.jar:na]
at org.springframework.boot.autoconfigure.mongo.MongoPropertiesClientSettingsBuilderCustomizer.applyHostAndPort(MongoPropertiesClientSettingsBuilderCustomizer.java:84) ~[spring-boot-autoconfigure-2.6.6.jar:2.6.6]
at org.springframework.boot.autoconfigure.mongo.MongoPropertiesClientSettingsBuilderCustomizer.customize(MongoPropertiesClientSettingsBuilderCustomizer.java:54) ~[spring-boot-autoconfigure-2.6.6.jar:2.6.6]
at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.customize(MongoClientFactorySupport.java:55) ~[spring-boot-autoconfigure-2.6.6.jar:2.6.6]
at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.createMongoClient(MongoClientFactorySupport.java:49) ~[spring-boot-autoconfigure-2.6.6.jar:2.6.6]
at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.mongo(MongoAutoConfiguration.java:55) ~[spring-boot-autoconfigure-2.6.6.jar:2.6.6]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.18.jar:5.3.18]
... 86 common frames omitted
Caused by: java.net.BindException: Cannot assign requested address: connect
at java.base/sun.nio.ch.Net.connect0(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.connect(Net.java:574) ~[na:na]
at java.base/sun.nio.ch.DatagramChannelImpl.connect(DatagramChannelImpl.java:1243) ~[na:na]
at java.base/sun.nio.ch.DatagramSocketAdaptor.connectInternal(DatagramSocketAdaptor.java:91) ~[na:na]
at java.base/sun.nio.ch.DatagramSocketAdaptor.connect(DatagramSocketAdaptor.java:118) ~[na:na]
... 107 common frames omitted
CodePudding user response:
Mobile data ip's are dynamic & continuously changes while wifi ip is static & not changes frequently.
Your application seems creating lot of connections on startup which causes issue while using wifi because of static IP ; while on other hand mobile data has dynamic ip & hence you are not experiencing this issue.
Check your application if that is the case. For windows, use below command to check how many ips are in use once you start your application, that will give you an idea.
netstat -ano
If above is correct for your application, consider using any available database connection pool libraries (e.g. Hikari etc.) for establishing connections so that your app will create limited connections.
CodePudding user response:
It has started working without any changes. The spring boot application logs now say
2022-07-05 16:35:44.318 INFO 12968 --- [ main] org.mongodb.driver.cluster: Cluster created with id ClusterId{value='XXXXXXXXXXXXXXXXXXXXXX', description='null'} and settings {hosts=[127.0.0.1:27017], srvHost=xxxxxxxxxxxxxxxx.bu2mw.mongodb.net, mode=LOAD_BALANCED, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2022-07-05 16:35:44.326 INFO 12968 --- [2mw.mongodb.net] org.mongodb.driver.cluster: SRV resolution completed with hosts: [xxxxxxxxxxxxxxxx-lb.bu2mw.mongodb.net:27017]