No buffer space available (maximum connections reached?): connect

Hi We are trying to insert around 10000 Documents to a MongoDB’s Collection. The data is basically read from the CSV file and stored into MongoDB local instance. Near to 8300 records got inserted and beyond this, we get the following error

07-10-2020 16:26:52.739 [main] INFO  org.mongodb.driver.cluster.info - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
07-10-2020 16:26:52.740 [main] INFO  org.mongodb.driver.cluster.info - Cluster description not yet available. Waiting for 30000 ms before timing out
07-10-2020 16:26:52.777 [cluster-ClusterId{value='5f7d9ef436efac69854c9165', description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster.info - Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
	at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70)
	at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
	at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: No buffer space available (maximum connections reached?): connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:83)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)

Was this the only thing connecting to your database at the time ?

Thanks for the reply Chris, Actually trying to read an Excel which has around 40000 records or rows, we wanted to insert these rows into MongoDB as a document and while doing so I am getting this error after storing 8100 records…

Was this the only thing connecting to your database at the time ?

The error message says you may be running out of connections. You can check the open sockets from the command line using:

netstat -n

Ensure that you are closing all sockets on both sides (in finally blocks). Remember that listening sockets remain open after you receive a connection. Also, if your code uses URL connections, make sure that they get disconnected. Don’t open and close sockets too quickly. The only reliable way to know whether any resource is available is to try to use it and handle the errors as they arise in the normal course of execution. Anything else is tantamount to fortune-telling.

For a better Java Socket - related performance you can use the java.nio API, but it’s far more complicated than java.net.